#include <bits/stdc++.h>
using i64 = long long;
constexpr int dx[] = {0, 0, -1, 1};
constexpr int dy[] = {1, -1, 0, 0};
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int h, w;
std::cin >> h >> w;
std::vector<std::string> a(h);
for (int i = 0; i < h; i++) {
std::cin >> a[i];
}
auto valid = [&](int x, int y) {
return 0 <= x && x < h && 0 <= y && y < w && a[x][y] != '.';
};
std::vector dist(h, std::vector<int>(w));
dist[0][0] = 1;
int ans = 0;
std::deque<std::pair<int, int>> que;
que.emplace_front(0, 0);
while (!que.empty()) {
auto [x, y] = que.front();
que.pop_front();
ans = std::max(ans, dist[x][y]);
for (int k = 0; k < 4; k++) {
int x1 = x + dx[k], y1 = y + dy[k];
if (valid(x1, y1) && !dist[x1][y1]) {
if (a[x][y] == a[x1][y1]) {
dist[x1][y1] = dist[x][y];
que.emplace_front(x1, y1);
} else {
dist[x1][y1] = dist[x][y] + 1;
que.emplace_back(x1, y1);
}
}
}
}
std::cout << 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 |
1372 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 |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
600 KB |
Output is correct |
10 |
Correct |
2 ms |
856 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
3 ms |
860 KB |
Output is correct |
13 |
Correct |
2 ms |
1112 KB |
Output is correct |
14 |
Correct |
1 ms |
856 KB |
Output is correct |
15 |
Correct |
6 ms |
1884 KB |
Output is correct |
16 |
Correct |
7 ms |
1884 KB |
Output is correct |
17 |
Correct |
5 ms |
1884 KB |
Output is correct |
18 |
Correct |
4 ms |
1372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
856 KB |
Output is correct |
2 |
Correct |
25 ms |
9584 KB |
Output is correct |
3 |
Correct |
136 ms |
96652 KB |
Output is correct |
4 |
Correct |
37 ms |
22864 KB |
Output is correct |
5 |
Correct |
75 ms |
54400 KB |
Output is correct |
6 |
Correct |
463 ms |
110432 KB |
Output is correct |
7 |
Correct |
2 ms |
856 KB |
Output is correct |
8 |
Correct |
1 ms |
1112 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 |
26 ms |
9656 KB |
Output is correct |
14 |
Correct |
15 ms |
5724 KB |
Output is correct |
15 |
Correct |
9 ms |
6232 KB |
Output is correct |
16 |
Correct |
13 ms |
4440 KB |
Output is correct |
17 |
Correct |
69 ms |
24524 KB |
Output is correct |
18 |
Correct |
40 ms |
24144 KB |
Output is correct |
19 |
Correct |
41 ms |
22612 KB |
Output is correct |
20 |
Correct |
33 ms |
20928 KB |
Output is correct |
21 |
Correct |
84 ms |
56568 KB |
Output is correct |
22 |
Correct |
75 ms |
54352 KB |
Output is correct |
23 |
Correct |
134 ms |
46740 KB |
Output is correct |
24 |
Correct |
78 ms |
55124 KB |
Output is correct |
25 |
Correct |
307 ms |
96656 KB |
Output is correct |
26 |
Correct |
503 ms |
124260 KB |
Output is correct |
27 |
Correct |
415 ms |
118252 KB |
Output is correct |
28 |
Correct |
455 ms |
110160 KB |
Output is correct |
29 |
Correct |
461 ms |
107724 KB |
Output is correct |
30 |
Correct |
456 ms |
110720 KB |
Output is correct |
31 |
Correct |
308 ms |
62288 KB |
Output is correct |
32 |
Correct |
486 ms |
113196 KB |
Output is correct |