#include <bits/stdc++.h>
using i64 = long long;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int h, w;
std::cin >> h >> w;
std::vector<std::string> g(h);
for (auto &s : g) {
std::cin >> s;
}
std::vector dis(h, std::vector<int>(w, -1));
dis[0][0] = 0;
std::deque<std::tuple<int, int>> q;
q.push_back({0, 0});
int ans = 0;
while (q.size()) {
auto [x, y] = q.front();
q.pop_front();
ans = std::max(ans, dis[x][y]);
for (int i = 0; i < 4; i++) {
int tx = x + dx[i];
int ty = y + dy[i];
if (tx < 0 || tx >= h || ty < 0 || ty >= w || g[tx][ty] == '.' || dis[tx][ty] != -1) {
continue;
}
if (g[x][y] == g[tx][ty]) {
dis[tx][ty] = dis[x][y];
q.push_front({tx, ty});
} else {
dis[tx][ty] = dis[x][y] + 1;
q.push_back({tx, ty});
}
}
}
std::cout << 1 + ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
1880 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
4 ms |
1552 KB |
Output is correct |
5 |
Correct |
2 ms |
860 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
860 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
3 ms |
856 KB |
Output is correct |
13 |
Correct |
2 ms |
856 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
6 ms |
1884 KB |
Output is correct |
16 |
Correct |
7 ms |
1880 KB |
Output is correct |
17 |
Correct |
5 ms |
1884 KB |
Output is correct |
18 |
Correct |
4 ms |
1628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
856 KB |
Output is correct |
2 |
Correct |
25 ms |
9560 KB |
Output is correct |
3 |
Correct |
150 ms |
89388 KB |
Output is correct |
4 |
Correct |
41 ms |
21252 KB |
Output is correct |
5 |
Correct |
92 ms |
50376 KB |
Output is correct |
6 |
Correct |
418 ms |
104152 KB |
Output is correct |
7 |
Correct |
1 ms |
856 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
860 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
27 ms |
9352 KB |
Output is correct |
14 |
Correct |
14 ms |
5468 KB |
Output is correct |
15 |
Correct |
11 ms |
6236 KB |
Output is correct |
16 |
Correct |
13 ms |
4184 KB |
Output is correct |
17 |
Correct |
67 ms |
22868 KB |
Output is correct |
18 |
Correct |
39 ms |
22612 KB |
Output is correct |
19 |
Correct |
39 ms |
21304 KB |
Output is correct |
20 |
Correct |
32 ms |
19972 KB |
Output is correct |
21 |
Correct |
82 ms |
51932 KB |
Output is correct |
22 |
Correct |
109 ms |
50276 KB |
Output is correct |
23 |
Correct |
144 ms |
43440 KB |
Output is correct |
24 |
Correct |
79 ms |
51028 KB |
Output is correct |
25 |
Correct |
179 ms |
89464 KB |
Output is correct |
26 |
Correct |
233 ms |
118584 KB |
Output is correct |
27 |
Correct |
358 ms |
118800 KB |
Output is correct |
28 |
Correct |
422 ms |
104352 KB |
Output is correct |
29 |
Correct |
404 ms |
100004 KB |
Output is correct |
30 |
Correct |
364 ms |
107156 KB |
Output is correct |
31 |
Correct |
334 ms |
57428 KB |
Output is correct |
32 |
Correct |
287 ms |
108556 KB |
Output is correct |