-
Notifications
You must be signed in to change notification settings - Fork 0
/
02589_보물섬.cpp
90 lines (81 loc) · 1.25 KB
/
02589_보물섬.cpp
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
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
struct pos
{
int r;
int c;
void init(int r, int c)
{
this->r = r;
this->c = c;
}
};
int N;
int M;
bool map[51][51];
bool in_range(pos p)
{
if (p.r < 0 || p.r >= N || p.c < 0 || p.c >= M)
return false;
return true;
}
int treasure_hunt(int r, int c)
{
queue<pos> q;
pos curr, next;
bool visit[51][51];
int d[] = {-1, 1, 0, 0};
int i, s, size, cnt = 0;
memset(visit, false, sizeof(visit));
curr.init(r, c);
visit[r][c] = true;
q.push(curr);
while (!q.empty())
{
size = q.size();
for (s = 0; s < size; s++)
{
curr = q.front();
q.pop();
for (i = 0; i < 4; i++)
{
next.init(curr.r + d[i], curr.c + d[3 - i]);
if (!in_range(next))
continue;
if (map[next.r][next.c] || visit[next.r][next.c])
continue;
visit[next.r][next.c] = true;
q.push(next);
}
}
cnt++;
}
return cnt - 1;
}
int main()
{
int i, j, answer = 0;
char c;
cin >> N >> M;
for (i = 0; i < N; i++)
{
for (j = 0; j < M; j++)
{
cin >> c;
if (c == 'W')
map[i][j] = 1;
}
}
for (i = 0; i < N; i++)
{
for (j = 0; j < M; j++)
{
if (!map[i][j])
answer = max(answer, treasure_hunt(i, j));
}
}
cout << answer << endl;
return 0;
}