#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 (odw[i.first]) continue;
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 |
28 ms |
17752 KB |
Output is correct |
2 |
Correct |
1 ms |
408 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
16 ms |
11816 KB |
Output is correct |
5 |
Correct |
3 ms |
3164 KB |
Output is correct |
6 |
Correct |
0 ms |
456 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
3 ms |
3164 KB |
Output is correct |
11 |
Correct |
4 ms |
3336 KB |
Output is correct |
12 |
Correct |
10 ms |
6608 KB |
Output is correct |
13 |
Correct |
4 ms |
3160 KB |
Output is correct |
14 |
Correct |
3 ms |
3164 KB |
Output is correct |
15 |
Correct |
20 ms |
15180 KB |
Output is correct |
16 |
Correct |
27 ms |
17560 KB |
Output is correct |
17 |
Correct |
16 ms |
10820 KB |
Output is correct |
18 |
Correct |
17 ms |
11868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2136 KB |
Output is correct |
2 |
Correct |
98 ms |
68440 KB |
Output is correct |
3 |
Correct |
444 ms |
557468 KB |
Output is correct |
4 |
Correct |
124 ms |
115340 KB |
Output is correct |
5 |
Correct |
357 ms |
368200 KB |
Output is correct |
6 |
Runtime error |
962 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 |
3148 KB |
Output is correct |
10 |
Correct |
1 ms |
1372 KB |
Output is correct |
11 |
Correct |
1 ms |
1884 KB |
Output is correct |
12 |
Correct |
2 ms |
1372 KB |
Output is correct |
13 |
Correct |
88 ms |
68372 KB |
Output is correct |
14 |
Correct |
48 ms |
40020 KB |
Output is correct |
15 |
Correct |
33 ms |
34752 KB |
Output is correct |
16 |
Correct |
41 ms |
32508 KB |
Output is correct |
17 |
Correct |
217 ms |
173920 KB |
Output is correct |
18 |
Correct |
129 ms |
135668 KB |
Output is correct |
19 |
Correct |
114 ms |
115496 KB |
Output is correct |
20 |
Correct |
95 ms |
123464 KB |
Output is correct |
21 |
Correct |
249 ms |
326488 KB |
Output is correct |
22 |
Correct |
355 ms |
368212 KB |
Output is correct |
23 |
Correct |
431 ms |
336980 KB |
Output is correct |
24 |
Correct |
261 ms |
323572 KB |
Output is correct |
25 |
Correct |
536 ms |
546928 KB |
Output is correct |
26 |
Correct |
1168 ms |
990176 KB |
Output is correct |
27 |
Runtime error |
939 ms |
1048576 KB |
Execution killed with signal 9 |
28 |
Runtime error |
979 ms |
1048576 KB |
Execution killed with signal 9 |
29 |
Runtime error |
986 ms |
1048576 KB |
Execution killed with signal 9 |
30 |
Runtime error |
1012 ms |
1048576 KB |
Execution killed with signal 9 |
31 |
Correct |
1587 ms |
738980 KB |
Output is correct |
32 |
Runtime error |
996 ms |
1048576 KB |
Execution killed with signal 9 |