#include<bits/stdc++.h>
using namespace std;
int main() {
cout.tie(nullptr)->sync_with_stdio(false);
int H, W; cin >> H >> W;
vector<string> grid(H);
for (string& S : grid) cin >> S;
vector<vector<int>> mindist(H, vector<int>(W, -1));
deque<array<int, 2>> bfs = {{0, 0}};
vector<int> dx = {1, 0, -1, 0};
vector<int> dy = {0, 1, 0, -1};
auto check = [&](int a, int b) -> bool {
if (a < 0 || a >= H || b < 0 || b >= W || grid[a][b] == '.') return false;
return true;
};
mindist[0][0] = 1;
int res = 0;
while (!bfs.empty()) {
auto [u, v] = bfs.front();
bfs.pop_front();
res = max(res, mindist[u][v]);
for (int d = 0; d < 4; ++d) {
int a = u + dx[d], b = v + dy[d];
if (!check(a, b)) continue;
if (mindist[a][b] == -1) {
int prev = mindist[u][v];
if (grid[u][v] == grid[a][b]) {
mindist[a][b] = prev;
bfs.push_front({a, b});
} else {
mindist[a][b] = 1 + prev;
bfs.push_back({a, b});
}
}
}
}
cout << res << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
1920 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
6 ms |
1516 KB |
Output is correct |
5 |
Correct |
2 ms |
876 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
748 KB |
Output is correct |
11 |
Correct |
2 ms |
620 KB |
Output is correct |
12 |
Correct |
4 ms |
876 KB |
Output is correct |
13 |
Correct |
2 ms |
876 KB |
Output is correct |
14 |
Correct |
2 ms |
876 KB |
Output is correct |
15 |
Correct |
9 ms |
1900 KB |
Output is correct |
16 |
Correct |
11 ms |
2176 KB |
Output is correct |
17 |
Correct |
8 ms |
1772 KB |
Output is correct |
18 |
Correct |
6 ms |
1516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
876 KB |
Output is correct |
2 |
Correct |
37 ms |
9708 KB |
Output is correct |
3 |
Correct |
201 ms |
96492 KB |
Output is correct |
4 |
Correct |
61 ms |
22892 KB |
Output is correct |
5 |
Correct |
121 ms |
54252 KB |
Output is correct |
6 |
Correct |
621 ms |
110600 KB |
Output is correct |
7 |
Correct |
2 ms |
876 KB |
Output is correct |
8 |
Correct |
2 ms |
876 KB |
Output is correct |
9 |
Correct |
2 ms |
748 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
2 ms |
876 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
38 ms |
9708 KB |
Output is correct |
14 |
Correct |
21 ms |
5740 KB |
Output is correct |
15 |
Correct |
15 ms |
6380 KB |
Output is correct |
16 |
Correct |
18 ms |
4256 KB |
Output is correct |
17 |
Correct |
101 ms |
24556 KB |
Output is correct |
18 |
Correct |
69 ms |
24300 KB |
Output is correct |
19 |
Correct |
60 ms |
22764 KB |
Output is correct |
20 |
Correct |
71 ms |
20972 KB |
Output is correct |
21 |
Correct |
115 ms |
56300 KB |
Output is correct |
22 |
Correct |
122 ms |
54380 KB |
Output is correct |
23 |
Correct |
195 ms |
46956 KB |
Output is correct |
24 |
Correct |
108 ms |
54764 KB |
Output is correct |
25 |
Correct |
433 ms |
96492 KB |
Output is correct |
26 |
Correct |
351 ms |
124156 KB |
Output is correct |
27 |
Correct |
476 ms |
131048 KB |
Output is correct |
28 |
Correct |
623 ms |
110688 KB |
Output is correct |
29 |
Correct |
614 ms |
108108 KB |
Output is correct |
30 |
Correct |
583 ms |
113768 KB |
Output is correct |
31 |
Correct |
534 ms |
62324 KB |
Output is correct |
32 |
Correct |
436 ms |
120868 KB |
Output is correct |