본문 바로가기
알고리즘/BFS

2589 보물섬

by tryotto 2020. 1. 19.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <stdio.h>
#include <string.h>
#include <queue>
 
using namespace std;
 
char arr[55][55= {0};
char chk[55][55= { 0 };
int dx[4= { 1,-1,0,0 };
int dy[4= { 0,0,1,-1 };
int row, col;
 
typedef struct _dot {
    int len;
    int y;
    int x;
}dot;
 
int bfs(int y, int x) {
    queue<dot> q;
    memcpy(chk, arr, sizeof(arr));
    q.push({ 0,y,x });
    chk[y][x] = -1;
 
    int len;
    while (!q.empty()) {
        len = q.front().len;
        int y = q.front().y;
        int x = q.front().x;
 
        q.pop();
 
        int rst = 0;
        for (int i = 0; i < 4; i++) {
            int xx = x + dx[i];
            int yy = y + dy[i];
 
            if (xx <= 0 || xx > col || yy <= 0 || yy > row) 
                continue;            
            if (chk[yy][xx] != 'L'
                continue;
            
            chk[yy][xx] = -1;
            q.push({ len + 1, yy, xx });        
        }
    }
    return len;
}
 
int main() {
    scanf("%d %d"&row, &col);
 
    for (int i = 1; i <= row; i++) {
        char tmp[55];
        scanf("%s"&tmp);
 
        for (int j = 1; j <= col; j++) {
            arr[i][j] = tmp[j - 1];
        }
    }
 
    int maxN = -1;
    for (int i = 1; i <= row; i++) {
        for (int j = 1; j <= col; j++) {
            if (arr[i][j] == 'L') {
                int tmp = bfs(i, j);
                if (tmp > maxN)
                    maxN = tmp;
            }
        }
    }
 
    printf("%d", maxN);
}
cs


'알고리즘 > BFS' 카테고리의 다른 글

9372 상근이의 여행  (0) 2020.01.20
5014 스타트링크  (0) 2020.01.20
2644 촌수계산  (0) 2020.01.18
2146 다리만들기  (0) 2020.01.18
7569 토마토  (0) 2020.01.14