#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
int H, W;
char grid[4000][4000];
int visited[4000][4000];
int main() {
cin >> H >> W;
for (int i = 0; i < H; ++i) {
string row;
cin >> row;
for (int j = 0; j < W; ++j) {
grid[i][j] = row[j];
}
}
deque<tuple<int, int>> q;
q.push_front({0, 0});
visited[0][0] = 1;
while (!q.empty()) {
auto [i, j] = q.front();
q.pop_front();
if (i > 0 && !visited[i - 1][j]) {
if (grid[i - 1][j] == grid[i][j]) {
visited[i - 1][j] = visited[i][j];
q.push_front({i - 1, j});
} else if (grid[i - 1][j] != '.') {
visited[i - 1][j] = visited[i][j] + 1;
q.push_back({i - 1, j});
}
}
if (i < H - 1 && !visited[i + 1][j]) {
if (grid[i + 1][j] == grid[i][j]) {
visited[i + 1][j] = visited[i][j];
q.push_front({i + 1, j});
} else if (grid[i + 1][j] != '.') {
visited[i + 1][j] = visited[i][j] + 1;
q.push_back({i + 1, j});
}
}
if (j > 0 && !visited[i][j - 1]) {
if (grid[i][j - 1] == grid[i][j]) {
visited[i][j - 1] = visited[i][j];
q.push_front({i, j - 1});
} else if (grid[i][j - 1] != '.') {
visited[i][j - 1] = visited[i][j] + 1;
q.push_back({i, j - 1});
}
}
if (j < W - 1 && !visited[i][j + 1]) {
if (grid[i][j + 1] == grid[i][j]) {
visited[i][j + 1] = visited[i][j];
q.push_front({i, j + 1});
} else if (grid[i][j + 1] != '.') {
visited[i][j + 1] = visited[i][j] + 1;
q.push_back({i, j + 1});
}
}
}
int answer = 0;
for (int i = 0; i < H; ++i) {
answer = max(answer, *max_element(visited[i], visited[i] + W));
}
cout << answer << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
7260 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
6 ms |
7512 KB |
Output is correct |
5 |
Correct |
3 ms |
3860 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2908 KB |
Output is correct |
10 |
Correct |
3 ms |
3420 KB |
Output is correct |
11 |
Correct |
3 ms |
3420 KB |
Output is correct |
12 |
Correct |
4 ms |
3932 KB |
Output is correct |
13 |
Correct |
3 ms |
3928 KB |
Output is correct |
14 |
Correct |
3 ms |
3932 KB |
Output is correct |
15 |
Correct |
10 ms |
7512 KB |
Output is correct |
16 |
Correct |
10 ms |
7260 KB |
Output is correct |
17 |
Correct |
11 ms |
7256 KB |
Output is correct |
18 |
Correct |
6 ms |
7260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
32172 KB |
Output is correct |
2 |
Correct |
43 ms |
14784 KB |
Output is correct |
3 |
Correct |
321 ms |
57936 KB |
Output is correct |
4 |
Correct |
90 ms |
31936 KB |
Output is correct |
5 |
Correct |
192 ms |
47028 KB |
Output is correct |
6 |
Correct |
526 ms |
91724 KB |
Output is correct |
7 |
Correct |
10 ms |
32856 KB |
Output is correct |
8 |
Correct |
10 ms |
32092 KB |
Output is correct |
9 |
Correct |
2 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
10 ms |
32604 KB |
Output is correct |
12 |
Correct |
1 ms |
3164 KB |
Output is correct |
13 |
Correct |
43 ms |
14780 KB |
Output is correct |
14 |
Correct |
25 ms |
12124 KB |
Output is correct |
15 |
Correct |
24 ms |
14268 KB |
Output is correct |
16 |
Correct |
19 ms |
7768 KB |
Output is correct |
17 |
Correct |
119 ms |
27248 KB |
Output is correct |
18 |
Correct |
92 ms |
33992 KB |
Output is correct |
19 |
Correct |
87 ms |
31824 KB |
Output is correct |
20 |
Correct |
73 ms |
23476 KB |
Output is correct |
21 |
Correct |
195 ms |
45756 KB |
Output is correct |
22 |
Correct |
182 ms |
47036 KB |
Output is correct |
23 |
Correct |
212 ms |
38716 KB |
Output is correct |
24 |
Correct |
202 ms |
42928 KB |
Output is correct |
25 |
Correct |
383 ms |
78528 KB |
Output is correct |
26 |
Correct |
355 ms |
121716 KB |
Output is correct |
27 |
Correct |
432 ms |
96104 KB |
Output is correct |
28 |
Correct |
499 ms |
91736 KB |
Output is correct |
29 |
Correct |
481 ms |
92100 KB |
Output is correct |
30 |
Correct |
453 ms |
92632 KB |
Output is correct |
31 |
Correct |
380 ms |
65448 KB |
Output is correct |
32 |
Correct |
413 ms |
93320 KB |
Output is correct |