Algorithm/Baekjoon
[Algorithm] Baekjoon 2589번 : 보물섬
MaxLevel
2022. 10. 8. 15:21
https://www.acmicpc.net/problem/2589
2589번: 보물섬
보물섬 지도를 발견한 후크 선장은 보물을 찾아나섰다. 보물섬 지도는 아래 그림과 같이 직사각형 모양이며 여러 칸으로 나뉘어져 있다. 각 칸은 육지(L)나 바다(W)로 표시되어 있다. 이 지도에서
www.acmicpc.net
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
struct Node
{
int y;
int x;
int count;
Node() {};
Node(int _y, int _x, int _count) : y(_y), x(_x), count(_count) {};
};
vector<string> tmap;
bool visitedMap[51][51];
vector<vector<int>> dir = { {-1,0}, {1,0}, {0,-1}, {0,1} };
int maxY;
int maxX;
int BFS(int row, int col)
{
Node startNode(row, col, 0);
queue<Node> q;
q.push(startNode);
visitedMap[row][col] = true;
int maxCount = 0;
while (!q.empty())
{
Node popedNode = q.front();
q.pop();
int curY = popedNode.y;
int curX = popedNode.x;
int curCount = popedNode.count;
maxCount = max(maxCount, curCount);
for (int i = 0; i < dir.size(); i++)
{
int nextY = curY + dir[i][0];
int nextX = curX + dir[i][1];
int nextCount = curCount + 1;
if (nextY < 0 || nextY >= maxY) continue;
if (nextX < 0 || nextX >= maxX) continue;
if (tmap[nextY][nextX] == 'W') continue;
if (visitedMap[nextY][nextX]) continue;
q.push(Node(nextY, nextX, nextCount));
visitedMap[nextY][nextX] = true;
}
}
return maxCount;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int row = 0;
int col = 0;
int result = 0;
cin >> row >> col;
tmap.resize(row);
maxY = row;
maxX = col;
for (int i = 0; i < row; i++)
{
cin >> tmap[i];
}
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (tmap[i][j] == 'L')
{
result = max(result, BFS(i, j));
memset(visitedMap, 0, sizeof(visitedMap));
}
}
}
cout << result << endl;
}
|
cs |
쉬운문제긴한데, 처음 문제 읽었을 땐 고민 좀 했던 문제.
기본적으로 브루트포스방식을 배제하고 생각하는 습관때문에 오히려 더 고민하는데에 시간이 걸렸다.
맵의 사이즈가 최대 50 x 50 밖에 안돼서 충분히 브루트포스로도 풀리는 문제이다.