#include <iostream>
#include <queue>
#include <tuple>
using namespace std;
int H, W;
char grid[4000][4000];
bool 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];
}
}
int answer = 1;
priority_queue<tuple<int, int, int>> queue;
queue.push({-1, 0, 0});
visited[0][0] = true;
while (!queue.empty()) {
auto [marker, i, j] = queue.top();
queue.pop();
answer = max(answer, -marker);
if (i > 0 && !visited[i - 1][j]) {
if (grid[i - 1][j] == grid[i][j]) {
visited[i - 1][j] = true;
queue.push({marker, i - 1, j});
} else if (grid[i - 1][j] != '.') {
visited[i - 1][j] = true;
queue.push({marker - 1, i - 1, j});
}
}
if (i < H - 1 && !visited[i + 1][j]) {
if (grid[i + 1][j] == grid[i][j]) {
visited[i + 1][j] = true;
queue.push({marker, i + 1, j});
} else if (grid[i + 1][j] != '.') {
visited[i + 1][j] = true;
queue.push({marker - 1, i + 1, j});
}
}
if (j > 0 && !visited[i][j - 1]) {
if (grid[i][j - 1] == grid[i][j]) {
visited[i][j - 1] = true;
queue.push({marker, i, j - 1});
} else if (grid[i][j - 1] != '.') {
visited[i][j - 1] = true;
queue.push({marker - 1, i, j - 1});
}
}
if (j < W - 1 && !visited[i][j + 1]) {
if (grid[i][j + 1] == grid[i][j]) {
visited[i][j + 1] = true;
queue.push({marker, i, j + 1});
} else if (grid[i][j + 1] != '.') {
visited[i][j + 1] = true;
queue.push({marker - 1, i, j + 1});
}
}
}
cout << answer << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
6488 KB |
Output is correct |
2 |
Correct |
2 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
29 ms |
6852 KB |
Output is correct |
5 |
Correct |
6 ms |
5468 KB |
Output is correct |
6 |
Correct |
1 ms |
2548 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 |
2652 KB |
Output is correct |
10 |
Correct |
7 ms |
5468 KB |
Output is correct |
11 |
Correct |
7 ms |
3420 KB |
Output is correct |
12 |
Correct |
18 ms |
5772 KB |
Output is correct |
13 |
Correct |
6 ms |
5468 KB |
Output is correct |
14 |
Correct |
5 ms |
5468 KB |
Output is correct |
15 |
Correct |
32 ms |
6492 KB |
Output is correct |
16 |
Correct |
47 ms |
6620 KB |
Output is correct |
17 |
Correct |
19 ms |
6464 KB |
Output is correct |
18 |
Correct |
27 ms |
6992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
31320 KB |
Output is correct |
2 |
Correct |
113 ms |
11348 KB |
Output is correct |
3 |
Correct |
443 ms |
31568 KB |
Output is correct |
4 |
Correct |
165 ms |
17864 KB |
Output is correct |
5 |
Correct |
226 ms |
26452 KB |
Output is correct |
6 |
Execution timed out |
2070 ms |
58164 KB |
Time limit exceeded |
7 |
Correct |
10 ms |
31580 KB |
Output is correct |
8 |
Correct |
9 ms |
31324 KB |
Output is correct |
9 |
Correct |
5 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
9 ms |
31324 KB |
Output is correct |
12 |
Correct |
1 ms |
2908 KB |
Output is correct |
13 |
Correct |
107 ms |
11548 KB |
Output is correct |
14 |
Correct |
62 ms |
10072 KB |
Output is correct |
15 |
Correct |
21 ms |
10328 KB |
Output is correct |
16 |
Correct |
57 ms |
5980 KB |
Output is correct |
17 |
Correct |
278 ms |
18620 KB |
Output is correct |
18 |
Correct |
84 ms |
18376 KB |
Output is correct |
19 |
Correct |
126 ms |
17856 KB |
Output is correct |
20 |
Correct |
103 ms |
15288 KB |
Output is correct |
21 |
Correct |
285 ms |
26808 KB |
Output is correct |
22 |
Correct |
217 ms |
26452 KB |
Output is correct |
23 |
Correct |
545 ms |
22672 KB |
Output is correct |
24 |
Correct |
215 ms |
26984 KB |
Output is correct |
25 |
Correct |
351 ms |
31680 KB |
Output is correct |
26 |
Correct |
1365 ms |
30544 KB |
Output is correct |
27 |
Execution timed out |
2027 ms |
36108 KB |
Time limit exceeded |
28 |
Execution timed out |
2009 ms |
58192 KB |
Time limit exceeded |
29 |
Execution timed out |
2067 ms |
45612 KB |
Time limit exceeded |
30 |
Execution timed out |
2047 ms |
44584 KB |
Time limit exceeded |
31 |
Correct |
1848 ms |
28248 KB |
Output is correct |
32 |
Correct |
1950 ms |
33404 KB |
Output is correct |