#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX = 4005;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int H, W;
char grid[MX][MX];
int dist[MX][MX];
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
for(int i = 0; i < MX; i++)
for(int j = 0; j < MX; j++)
dist[i][j] = -1;
cin >> H >> W;
for(int i = 0; i < H; i++) {
for(int j = 0; j < W; j++) {
cin >> grid[i][j];
}
}
deque<pair<int,int>> dq;
dq.push_front({0, 0});
dist[0][0] = 0;
int ans = 0;
while(!dq.empty()) {
auto [x, y] = dq.front(); dq.pop_front();
ans = max(ans, dist[x][y]);
for(int k = 0; k < 4; k++) {
int nx = x + dx[k], ny = y + dy[k];
if(nx >= 0 && nx < H && ny >= 0 && ny < W && grid[nx][ny] != '.' && dist[nx][ny] == -1) {
if(grid[x][y] == grid[nx][ny]) {
dist[nx][ny] = dist[x][y];
dq.push_front({nx, ny});
} else {
dist[nx][ny] = dist[x][y] + 1;
dq.push_back({nx, ny});
}
}
}
}
cout << ans + 1 << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
65244 KB |
Output is correct |
2 |
Correct |
22 ms |
63084 KB |
Output is correct |
3 |
Correct |
23 ms |
63228 KB |
Output is correct |
4 |
Correct |
29 ms |
65316 KB |
Output is correct |
5 |
Correct |
25 ms |
64268 KB |
Output is correct |
6 |
Correct |
25 ms |
63088 KB |
Output is correct |
7 |
Correct |
23 ms |
63208 KB |
Output is correct |
8 |
Correct |
23 ms |
63244 KB |
Output is correct |
9 |
Correct |
23 ms |
63444 KB |
Output is correct |
10 |
Correct |
25 ms |
64048 KB |
Output is correct |
11 |
Correct |
23 ms |
63956 KB |
Output is correct |
12 |
Correct |
26 ms |
64284 KB |
Output is correct |
13 |
Correct |
24 ms |
64272 KB |
Output is correct |
14 |
Correct |
25 ms |
64220 KB |
Output is correct |
15 |
Correct |
31 ms |
65288 KB |
Output is correct |
16 |
Correct |
31 ms |
65228 KB |
Output is correct |
17 |
Correct |
29 ms |
65176 KB |
Output is correct |
18 |
Correct |
29 ms |
65268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
78100 KB |
Output is correct |
2 |
Correct |
59 ms |
69480 KB |
Output is correct |
3 |
Correct |
265 ms |
94308 KB |
Output is correct |
4 |
Correct |
87 ms |
74036 KB |
Output is correct |
5 |
Correct |
171 ms |
83600 KB |
Output is correct |
6 |
Correct |
613 ms |
108412 KB |
Output is correct |
7 |
Correct |
29 ms |
78804 KB |
Output is correct |
8 |
Correct |
33 ms |
78012 KB |
Output is correct |
9 |
Correct |
24 ms |
63180 KB |
Output is correct |
10 |
Correct |
23 ms |
63120 KB |
Output is correct |
11 |
Correct |
28 ms |
78388 KB |
Output is correct |
12 |
Correct |
24 ms |
63648 KB |
Output is correct |
13 |
Correct |
65 ms |
69408 KB |
Output is correct |
14 |
Correct |
44 ms |
67444 KB |
Output is correct |
15 |
Correct |
42 ms |
67980 KB |
Output is correct |
16 |
Correct |
41 ms |
65300 KB |
Output is correct |
17 |
Correct |
140 ms |
74972 KB |
Output is correct |
18 |
Correct |
96 ms |
74812 KB |
Output is correct |
19 |
Correct |
90 ms |
74144 KB |
Output is correct |
20 |
Correct |
89 ms |
73292 KB |
Output is correct |
21 |
Correct |
167 ms |
84260 KB |
Output is correct |
22 |
Correct |
176 ms |
83640 KB |
Output is correct |
23 |
Correct |
219 ms |
80420 KB |
Output is correct |
24 |
Correct |
166 ms |
84172 KB |
Output is correct |
25 |
Correct |
450 ms |
94360 KB |
Output is correct |
26 |
Correct |
419 ms |
138852 KB |
Output is correct |
27 |
Correct |
533 ms |
128972 KB |
Output is correct |
28 |
Correct |
677 ms |
108316 KB |
Output is correct |
29 |
Correct |
664 ms |
105988 KB |
Output is correct |
30 |
Correct |
617 ms |
112892 KB |
Output is correct |
31 |
Correct |
456 ms |
86072 KB |
Output is correct |
32 |
Correct |
484 ms |
118720 KB |
Output is correct |