#include <bits/stdc++.h>
using namespace std;
const int mxN = 4002;
char mat[mxN][mxN];
int dist[mxN][mxN];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
const int INF = 1e9 + 12;
int main() {
int h, w;
cin >> h >> w;
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
cin >> mat[i][j];
dist[i][j] = INF;
}
}
deque<pair<int, int>> q;
q.push_front({h-1, w-1});
dist[h-1][w-1] = 1;
auto check = [&](int x, int y) {
return x >= 0 && x < h && y >= 0 && y < w;
};
while(!q.empty()) {
auto tren = q.front();
q.pop_front();
for(int i = 0; i < 4; ++i) {
if(!check(tren.first+dx[i], tren.second+dy[i])) continue;
int nx = tren.first+dx[i];
int ny = tren.second+dy[i];
if(mat[nx][ny] == '.') continue;
if(dist[nx][ny] != INF) continue;
if(mat[nx][ny] != mat[tren.first][tren.second]) {
dist[nx][ny] = dist[tren.first][tren.second] + 1;
q.push_back({nx, ny});
} else {
dist[nx][ny] = dist[tren.first][tren.second];
q.push_front({nx, ny});
}
}
}
int ans = 0;
for(int i = 0; i < h; ++i) {
for(int j = 0; j < w; ++j) {
if(dist[i][j] == INF) continue;
ans = max(ans, dist[i][j]);
}
}
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
5304 KB |
Output is correct |
2 |
Correct |
0 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
680 KB |
Output is correct |
4 |
Correct |
21 ms |
5084 KB |
Output is correct |
5 |
Correct |
8 ms |
2972 KB |
Output is correct |
6 |
Correct |
0 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
716 KB |
Output is correct |
8 |
Correct |
1 ms |
716 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
6 ms |
2484 KB |
Output is correct |
11 |
Correct |
4 ms |
2124 KB |
Output is correct |
12 |
Correct |
9 ms |
3032 KB |
Output is correct |
13 |
Correct |
7 ms |
3004 KB |
Output is correct |
14 |
Correct |
7 ms |
2924 KB |
Output is correct |
15 |
Correct |
23 ms |
5456 KB |
Output is correct |
16 |
Correct |
23 ms |
5356 KB |
Output is correct |
17 |
Correct |
20 ms |
5196 KB |
Output is correct |
18 |
Correct |
15 ms |
5068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
30760 KB |
Output is correct |
2 |
Correct |
116 ms |
17728 KB |
Output is correct |
3 |
Correct |
1034 ms |
83260 KB |
Output is correct |
4 |
Correct |
258 ms |
33632 KB |
Output is correct |
5 |
Correct |
589 ms |
64944 KB |
Output is correct |
6 |
Correct |
1414 ms |
96268 KB |
Output is correct |
7 |
Correct |
16 ms |
32204 KB |
Output is correct |
8 |
Correct |
15 ms |
30848 KB |
Output is correct |
9 |
Correct |
5 ms |
684 KB |
Output is correct |
10 |
Correct |
2 ms |
476 KB |
Output is correct |
11 |
Correct |
15 ms |
31620 KB |
Output is correct |
12 |
Correct |
2 ms |
1592 KB |
Output is correct |
13 |
Correct |
117 ms |
17776 KB |
Output is correct |
14 |
Correct |
73 ms |
11820 KB |
Output is correct |
15 |
Correct |
77 ms |
13032 KB |
Output is correct |
16 |
Correct |
49 ms |
6468 KB |
Output is correct |
17 |
Correct |
310 ms |
36112 KB |
Output is correct |
18 |
Correct |
279 ms |
35692 KB |
Output is correct |
19 |
Correct |
265 ms |
33524 KB |
Output is correct |
20 |
Correct |
228 ms |
31020 KB |
Output is correct |
21 |
Correct |
610 ms |
66740 KB |
Output is correct |
22 |
Correct |
589 ms |
64896 KB |
Output is correct |
23 |
Correct |
592 ms |
55172 KB |
Output is correct |
24 |
Correct |
614 ms |
66532 KB |
Output is correct |
25 |
Correct |
1204 ms |
83360 KB |
Output is correct |
26 |
Correct |
1278 ms |
123564 KB |
Output is correct |
27 |
Correct |
1411 ms |
98844 KB |
Output is correct |
28 |
Correct |
1420 ms |
95896 KB |
Output is correct |
29 |
Correct |
1420 ms |
94380 KB |
Output is correct |
30 |
Correct |
1428 ms |
96096 KB |
Output is correct |
31 |
Correct |
943 ms |
68568 KB |
Output is correct |
32 |
Correct |
1386 ms |
103124 KB |
Output is correct |