/*
BoopyTheNoob, 29.10.2023, 20:55 +8
Somehow there's an error if the variables aren't declared globally.
*/
#include <bits/stdc++.h>
using namespace std;
int main (void) {
int dx[4]{1, -1, 0, 0}, dy[4]{0, 0, 1, -1};
int N, M, dist[4000][4000], ans = 1;
string grid[4000];
iostream::sync_with_stdio(false);
cin.tie(0);
cin >> N >> M;
//vector<string> grid(N);
for (int i = 0; i < N; i++)
cin >> grid[i];
//vector<vector<int>> dist(N, vector<int>(M, 0));
deque<vector<int>> bfs;
bfs.push_back({0, 0});
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
dist[i][j] = 0;
dist[0][0] = 1;
while (!bfs.empty()) {
int x = bfs.front()[0], y = bfs.front()[1];
bfs.pop_front();
ans = max(ans, dist[x][y]);
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
/*if (nx < 0 || nx >= N || ny < 0 || ny >= M)
continue;
if (dist[nx][ny] != 0 || grid[nx][ny] == '.')
continue;
pair<int, int> next = {nx, ny};
if (grid[x][y] != grid[nx][ny]) {
dist[nx][ny] = dist[x][y] + 1;
bfs.push_back({nx, ny});
} else {
dist[nx][ny] = dist[x][y];
bfs.push_front({nx, ny});
}*/
if (nx >= 0 && nx < N && ny >= 0 && ny < M && grid[nx][ny] != '.' && dist[nx][ny] == 0) {
if (grid[nx][ny] != grid[x][y]) {
dist[nx][ny] = dist[x][y] + 1;
bfs.push_back({nx, ny});
} else {
dist[nx][ny] = dist[x][y];
bfs.push_front({nx, ny});
}
}
}
}
/*for (auto x: dist) {
for (auto y: x) {
cout << y << " ";
}
cout << endl;
}*/
cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
64084 KB |
Output is correct |
2 |
Correct |
28 ms |
63068 KB |
Output is correct |
3 |
Correct |
33 ms |
63068 KB |
Output is correct |
4 |
Correct |
36 ms |
64712 KB |
Output is correct |
5 |
Correct |
30 ms |
63064 KB |
Output is correct |
6 |
Correct |
29 ms |
63116 KB |
Output is correct |
7 |
Correct |
29 ms |
63060 KB |
Output is correct |
8 |
Correct |
29 ms |
63068 KB |
Output is correct |
9 |
Correct |
31 ms |
63212 KB |
Output is correct |
10 |
Correct |
36 ms |
63088 KB |
Output is correct |
11 |
Correct |
33 ms |
63460 KB |
Output is correct |
12 |
Correct |
34 ms |
63324 KB |
Output is correct |
13 |
Correct |
30 ms |
63056 KB |
Output is correct |
14 |
Correct |
35 ms |
63056 KB |
Output is correct |
15 |
Correct |
39 ms |
63572 KB |
Output is correct |
16 |
Correct |
43 ms |
63836 KB |
Output is correct |
17 |
Correct |
36 ms |
63316 KB |
Output is correct |
18 |
Correct |
40 ms |
64604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
63036 KB |
Output is correct |
2 |
Correct |
66 ms |
64852 KB |
Output is correct |
3 |
Correct |
234 ms |
80720 KB |
Output is correct |
4 |
Correct |
81 ms |
66972 KB |
Output is correct |
5 |
Correct |
250 ms |
72904 KB |
Output is correct |
6 |
Correct |
1104 ms |
172456 KB |
Output is correct |
7 |
Correct |
30 ms |
63068 KB |
Output is correct |
8 |
Correct |
31 ms |
63152 KB |
Output is correct |
9 |
Correct |
31 ms |
63312 KB |
Output is correct |
10 |
Correct |
29 ms |
63068 KB |
Output is correct |
11 |
Correct |
30 ms |
63076 KB |
Output is correct |
12 |
Correct |
29 ms |
63068 KB |
Output is correct |
13 |
Correct |
66 ms |
64856 KB |
Output is correct |
14 |
Correct |
51 ms |
64080 KB |
Output is correct |
15 |
Correct |
44 ms |
64080 KB |
Output is correct |
16 |
Correct |
48 ms |
63824 KB |
Output is correct |
17 |
Correct |
152 ms |
67644 KB |
Output is correct |
18 |
Correct |
95 ms |
67252 KB |
Output is correct |
19 |
Correct |
77 ms |
67164 KB |
Output is correct |
20 |
Correct |
77 ms |
66788 KB |
Output is correct |
21 |
Correct |
156 ms |
73228 KB |
Output is correct |
22 |
Correct |
239 ms |
72788 KB |
Output is correct |
23 |
Correct |
236 ms |
71760 KB |
Output is correct |
24 |
Correct |
169 ms |
72940 KB |
Output is correct |
25 |
Correct |
440 ms |
80724 KB |
Output is correct |
26 |
Correct |
845 ms |
416664 KB |
Output is correct |
27 |
Correct |
978 ms |
261104 KB |
Output is correct |
28 |
Correct |
1096 ms |
172200 KB |
Output is correct |
29 |
Correct |
1097 ms |
161856 KB |
Output is correct |
30 |
Correct |
1016 ms |
201908 KB |
Output is correct |
31 |
Correct |
643 ms |
77116 KB |
Output is correct |
32 |
Correct |
778 ms |
184676 KB |
Output is correct |