#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int h, w;
cin >> h >> w;
vector<string> s (h);
for (int i = 0; i < h; i++) {
cin >> s[i];
}
vector<vector<pair<int, bool>>> g (h*w);
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (s[i][j] == '.') continue;
if (i != 0 && s[i-1][j] != '.') {
g[i*w+j].push_back({(i-1)*w+j, s[i][j]==s[i-1][j]});
}
if (j != 0 && s[i][j-1] != '.') {
g[i*w+j].push_back({i*w+j-1, s[i][j]==s[i][j-1]});
}
if (i != h-1 && s[i+1][j] != '.') {
g[i*w+j].push_back({(i+1)*w+j, s[i][j]==s[i+1][j]});
}
if (j != w-1 && s[i][j+1] != '.') {
g[i*w+j].push_back({i*w+j+1, s[i][j]==s[i][j+1]});
}
}
}
deque<pair<int, int>> q;
vector<bool> odw (h*w, 0);
q.push_back({0, 1});
int maxd = 0;
while (!q.empty()) {
auto x = q.front();
q.pop_front();
if (odw[x.first]) {
continue;
}
maxd = max(maxd, x.second);
odw[x.first] = 1;
for (auto i : g[x.first]) {
if (i.second) {
q.push_front({i.first, x.second});
}
else {
q.push_back({i.first, x.second+1});
}
}
}
cout << maxd << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
17756 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
18 ms |
12056 KB |
Output is correct |
5 |
Correct |
4 ms |
3160 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
452 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
10 |
Correct |
4 ms |
3028 KB |
Output is correct |
11 |
Correct |
4 ms |
3420 KB |
Output is correct |
12 |
Correct |
10 ms |
6488 KB |
Output is correct |
13 |
Correct |
3 ms |
3164 KB |
Output is correct |
14 |
Correct |
3 ms |
3164 KB |
Output is correct |
15 |
Correct |
23 ms |
15072 KB |
Output is correct |
16 |
Correct |
30 ms |
17752 KB |
Output is correct |
17 |
Correct |
13 ms |
10844 KB |
Output is correct |
18 |
Correct |
20 ms |
12124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2136 KB |
Output is correct |
2 |
Correct |
89 ms |
68840 KB |
Output is correct |
3 |
Correct |
440 ms |
563128 KB |
Output is correct |
4 |
Correct |
121 ms |
117072 KB |
Output is correct |
5 |
Correct |
399 ms |
371728 KB |
Output is correct |
6 |
Runtime error |
954 ms |
1048576 KB |
Execution killed with signal 9 |
7 |
Correct |
2 ms |
1880 KB |
Output is correct |
8 |
Correct |
2 ms |
2140 KB |
Output is correct |
9 |
Correct |
3 ms |
3288 KB |
Output is correct |
10 |
Correct |
1 ms |
1372 KB |
Output is correct |
11 |
Correct |
1 ms |
1884 KB |
Output is correct |
12 |
Correct |
1 ms |
1384 KB |
Output is correct |
13 |
Correct |
85 ms |
68828 KB |
Output is correct |
14 |
Correct |
47 ms |
40152 KB |
Output is correct |
15 |
Correct |
30 ms |
34832 KB |
Output is correct |
16 |
Correct |
41 ms |
32744 KB |
Output is correct |
17 |
Correct |
223 ms |
175476 KB |
Output is correct |
18 |
Correct |
140 ms |
137556 KB |
Output is correct |
19 |
Correct |
113 ms |
116876 KB |
Output is correct |
20 |
Correct |
112 ms |
124684 KB |
Output is correct |
21 |
Correct |
252 ms |
330232 KB |
Output is correct |
22 |
Correct |
365 ms |
371584 KB |
Output is correct |
23 |
Correct |
437 ms |
340048 KB |
Output is correct |
24 |
Correct |
260 ms |
327024 KB |
Output is correct |
25 |
Correct |
523 ms |
553024 KB |
Output is correct |
26 |
Runtime error |
972 ms |
1048576 KB |
Execution killed with signal 9 |
27 |
Runtime error |
933 ms |
1048576 KB |
Execution killed with signal 9 |
28 |
Runtime error |
937 ms |
1048576 KB |
Execution killed with signal 9 |
29 |
Runtime error |
983 ms |
1048576 KB |
Execution killed with signal 9 |
30 |
Runtime error |
945 ms |
1048576 KB |
Execution killed with signal 9 |
31 |
Correct |
1587 ms |
743524 KB |
Output is correct |
32 |
Runtime error |
929 ms |
1048576 KB |
Execution killed with signal 9 |