#include <bits/stdc++.h>
#define MAX_D 4001
#define MAX_D 4001
using namespace std;
int h, w;
char meadow[MAX_D][MAX_D];
int component[MAX_D][MAX_D];
void floodfill(int x, int y, int cmp) {
component[x][y] = cmp;
if(x + 1 < h && meadow[x + 1][y] == meadow[x][y] && component[x + 1][y] == 0) {
floodfill(x + 1, y, cmp);
}
if(x - 1 >= 0 && meadow[x - 1][y] == meadow[x][y] && component[x - 1][y] == 0) {
floodfill(x - 1, y, cmp);
}
if(y + 1 < w && meadow[x][y + 1] == meadow[x][y] && component[x][y + 1] == 0) {
floodfill(x, y + 1, cmp);
}
if(y - 1 >= 0 && meadow[x][y - 1] == meadow[x][y] && component[x][y - 1] == 0) {
floodfill(x, y - 1, cmp);
}
}
int main() {
string tmps;
cin >> h >> w;
for(int i = 0; i < h; i++) {
cin >> tmps;
for(int j = 0; j < w; j++) {
meadow[i][j] = tmps[j];
}
}
int cnt = 1;
for(int i = 0; i < h; i++) {
for(int j = 0; j < w; j++) {
if(component[i][j] == 0 && meadow[i][j] != '.') {
floodfill(i, j, cnt);
cnt++;
}
}
}
unordered_set<int> graph[cnt];
for(int i = 0; i < h; i++) {
for(int j = 0; j < w; j++) {
if(meadow[i][j] == '.') {
continue;
}
if(i + 1 < h && meadow[i + 1][j] != '.' && component[i + 1][j] != component[i][j] && graph[component[i + 1][j]].count(component[i][j]) == 0) {
graph[component[i + 1][j]].insert(component[i][j]);
graph[component[i][j]].insert(component[i + 1][j]);
}
if(i - 1 >= 0 && meadow[i - 1][j] != '.' && component[i - 1][j] != component[i][j] && graph[component[i - 1][j]].count(component[i][j]) == 0) {
graph[component[i - 1][j]].insert(component[i][j]);
graph[component[i][j]].insert(component[i - 1][j]);
}
if(j + 1 < w && meadow[i][j + 1] != '.' && component[i][j + 1] != component[i][j] && graph[component[i][j + 1]].count(component[i][j]) == 0) {
graph[component[i][j + 1]].insert(component[i][j]);
graph[component[i][j]].insert(component[i][j + 1]);
}
if(j - 1 >= 0 && meadow[i][j - 1] != '.' && component[i][j - 1] != component[i][j] && graph[component[i][j - 1]].count(component[i][j]) == 0) {
graph[component[i][j - 1]].insert(component[i][j]);
graph[component[i][j]].insert(component[i][j - 1]);
}
}
}
int d[cnt];
fill(d, d + cnt, -1);
queue<pair<int, int>> q;
d[component[0][0]] = 0;
q.push({component[0][0], 0});
while(!q.empty()) {
pair<int, int> curr_node = q.front();
q.pop();
for(auto iter : graph[curr_node.first]) {
if(d[iter] == -1) {
d[iter] = curr_node.second + 1;
q.push({iter, curr_node.second + 1});
}
}
}
int ans = 1;
for(int i = 0; i < cnt; i++) {
ans = max(ans, d[i] + 1);
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
16476 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
11 ms |
8888 KB |
Output is correct |
5 |
Correct |
7 ms |
5720 KB |
Output is correct |
6 |
Correct |
2 ms |
2648 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
3 ms |
2908 KB |
Output is correct |
10 |
Correct |
7 ms |
5468 KB |
Output is correct |
11 |
Correct |
3 ms |
3932 KB |
Output is correct |
12 |
Correct |
15 ms |
9260 KB |
Output is correct |
13 |
Correct |
6 ms |
5724 KB |
Output is correct |
14 |
Correct |
7 ms |
5724 KB |
Output is correct |
15 |
Correct |
31 ms |
16220 KB |
Output is correct |
16 |
Correct |
50 ms |
16468 KB |
Output is correct |
17 |
Correct |
27 ms |
15708 KB |
Output is correct |
18 |
Correct |
11 ms |
8796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
33628 KB |
Output is correct |
2 |
Correct |
165 ms |
62212 KB |
Output is correct |
3 |
Correct |
1031 ms |
399800 KB |
Output is correct |
4 |
Correct |
224 ms |
91476 KB |
Output is correct |
5 |
Runtime error |
1689 ms |
1048576 KB |
Execution killed with signal 9 |
6 |
Correct |
1264 ms |
249916 KB |
Output is correct |
7 |
Correct |
16 ms |
34140 KB |
Output is correct |
8 |
Correct |
13 ms |
33624 KB |
Output is correct |
9 |
Correct |
6 ms |
4444 KB |
Output is correct |
10 |
Correct |
2 ms |
3416 KB |
Output is correct |
11 |
Correct |
14 ms |
33624 KB |
Output is correct |
12 |
Correct |
6 ms |
5756 KB |
Output is correct |
13 |
Correct |
173 ms |
63396 KB |
Output is correct |
14 |
Correct |
107 ms |
40612 KB |
Output is correct |
15 |
Correct |
124 ms |
47952 KB |
Output is correct |
16 |
Correct |
71 ms |
30232 KB |
Output is correct |
17 |
Correct |
383 ms |
153172 KB |
Output is correct |
18 |
Correct |
416 ms |
163668 KB |
Output is correct |
19 |
Correct |
227 ms |
93776 KB |
Output is correct |
20 |
Correct |
249 ms |
103248 KB |
Output is correct |
21 |
Correct |
615 ms |
255284 KB |
Output is correct |
22 |
Runtime error |
1567 ms |
1048576 KB |
Execution killed with signal 9 |
23 |
Correct |
735 ms |
281680 KB |
Output is correct |
24 |
Correct |
782 ms |
393416 KB |
Output is correct |
25 |
Correct |
1889 ms |
612936 KB |
Output is correct |
26 |
Runtime error |
803 ms |
1048576 KB |
Execution killed with signal 9 |
27 |
Correct |
958 ms |
491444 KB |
Output is correct |
28 |
Correct |
1205 ms |
250056 KB |
Output is correct |
29 |
Correct |
1014 ms |
198064 KB |
Output is correct |
30 |
Correct |
1024 ms |
276672 KB |
Output is correct |
31 |
Correct |
1842 ms |
450752 KB |
Output is correct |
32 |
Correct |
1052 ms |
548620 KB |
Output is correct |