#include <bits/stdc++.h>
using namespace std;
int main() {
int H, W;
cin >> H >> W;
vector<string> S(H);
for (auto &v : S) {
cin >> v;
}
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
vector<vector<int>> dist(H, vector<int>(W, INT_MAX));
deque<pair<int, int>> q;
q.push_back({0, 0});
int ans = dist[0][0] = 1;
while (!q.empty()) {
auto [x, y] = q.front();
q.pop_front();
for (int d = 0; d < 4; d++) {
int i = x + dx[d], j = y + dy[d];
if (i >= 0 && i < H && j >= 0 && j < W && S[i][j] != '.' && dist[i][j] == INT_MAX) {
if (S[i][j] == S[x][y]) {
q.push_front({i, j});
dist[i][j] = dist[x][y];
} else {
q.push_back({i, j});
ans = max(ans, dist[i][j] = dist[x][y] + 1);
}
}
}
}
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
2052 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
6 ms |
1464 KB |
Output is correct |
5 |
Correct |
3 ms |
852 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
312 KB |
Output is correct |
10 |
Correct |
2 ms |
648 KB |
Output is correct |
11 |
Correct |
2 ms |
568 KB |
Output is correct |
12 |
Correct |
5 ms |
820 KB |
Output is correct |
13 |
Correct |
3 ms |
860 KB |
Output is correct |
14 |
Correct |
3 ms |
852 KB |
Output is correct |
15 |
Correct |
10 ms |
2060 KB |
Output is correct |
16 |
Correct |
12 ms |
2040 KB |
Output is correct |
17 |
Correct |
8 ms |
1964 KB |
Output is correct |
18 |
Correct |
6 ms |
1476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
692 KB |
Output is correct |
2 |
Correct |
45 ms |
10312 KB |
Output is correct |
3 |
Correct |
351 ms |
108956 KB |
Output is correct |
4 |
Correct |
88 ms |
25872 KB |
Output is correct |
5 |
Correct |
222 ms |
55840 KB |
Output is correct |
6 |
Correct |
618 ms |
122572 KB |
Output is correct |
7 |
Correct |
3 ms |
724 KB |
Output is correct |
8 |
Correct |
3 ms |
724 KB |
Output is correct |
9 |
Correct |
2 ms |
724 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
3 ms |
700 KB |
Output is correct |
12 |
Correct |
2 ms |
488 KB |
Output is correct |
13 |
Correct |
44 ms |
10392 KB |
Output is correct |
14 |
Correct |
26 ms |
6452 KB |
Output is correct |
15 |
Correct |
32 ms |
7132 KB |
Output is correct |
16 |
Correct |
23 ms |
4216 KB |
Output is correct |
17 |
Correct |
125 ms |
27788 KB |
Output is correct |
18 |
Correct |
106 ms |
27540 KB |
Output is correct |
19 |
Correct |
102 ms |
25872 KB |
Output is correct |
20 |
Correct |
78 ms |
23812 KB |
Output is correct |
21 |
Correct |
209 ms |
57772 KB |
Output is correct |
22 |
Correct |
238 ms |
55736 KB |
Output is correct |
23 |
Correct |
238 ms |
47788 KB |
Output is correct |
24 |
Correct |
196 ms |
56588 KB |
Output is correct |
25 |
Correct |
517 ms |
108956 KB |
Output is correct |
26 |
Correct |
389 ms |
123812 KB |
Output is correct |
27 |
Correct |
541 ms |
135548 KB |
Output is correct |
28 |
Correct |
699 ms |
122580 KB |
Output is correct |
29 |
Correct |
647 ms |
120924 KB |
Output is correct |
30 |
Correct |
580 ms |
124556 KB |
Output is correct |
31 |
Correct |
533 ms |
63120 KB |
Output is correct |
32 |
Correct |
471 ms |
124244 KB |
Output is correct |