#include <bits/stdc++.h>
using namespace std;
int di[4] = {1, -1, 0, 0};
int dj[4] = {0, 0, 1, -1};
int main() {
cin.tie(0)->sync_with_stdio(0);
int H, W;
cin >> H >> W;
vector<string> grid(H);
for (int i=0; i<H; i++) cin >> grid[i];
vector<vector<int>> dist(H, vector<int>(W, -1));
deque<pair<int,int>> Q;
dist[H-1][W-1] = 1;
Q.push_back({H-1, W-1});
while (Q.size()) {
auto [i, j] = Q.front(); Q.pop_front();
for (int k=0; k<4; k++) {
int i2 = i + di[k];
int j2 = j + dj[k];
if (i2 < 0 || j2 < 0 || i2 >= H || j2 >= W) continue;
if (grid[i2][j2] == '.') continue;
int d = dist[i][j] + (grid[i][j] != grid[i2][j2]);
if (dist[i2][j2] == -1 || d < dist[i2][j2]) {
dist[i2][j2] = d;
if (grid[i][j] == grid[i2][j2]) Q.push_front({i2, j2});
else Q.push_back({i2, j2});
}
}
}
int ans = 0;
for (int i=0; i<H; i++) {
for (int j=0; j<W; j++) {
ans = max(ans, dist[i][j]);
}
}
cout << ans << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
1628 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
5 ms |
1372 KB |
Output is correct |
5 |
Correct |
2 ms |
860 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
3 ms |
860 KB |
Output is correct |
13 |
Correct |
1 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
7 ms |
1628 KB |
Output is correct |
16 |
Correct |
8 ms |
1880 KB |
Output is correct |
17 |
Correct |
6 ms |
1624 KB |
Output is correct |
18 |
Correct |
6 ms |
1368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
860 KB |
Output is correct |
2 |
Correct |
28 ms |
8304 KB |
Output is correct |
3 |
Correct |
161 ms |
81020 KB |
Output is correct |
4 |
Correct |
42 ms |
19036 KB |
Output is correct |
5 |
Correct |
125 ms |
45608 KB |
Output is correct |
6 |
Correct |
496 ms |
94000 KB |
Output is correct |
7 |
Correct |
1 ms |
856 KB |
Output is correct |
8 |
Correct |
1 ms |
856 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
860 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
32 ms |
8284 KB |
Output is correct |
14 |
Correct |
15 ms |
4956 KB |
Output is correct |
15 |
Correct |
10 ms |
5468 KB |
Output is correct |
16 |
Correct |
14 ms |
3688 KB |
Output is correct |
17 |
Correct |
78 ms |
20572 KB |
Output is correct |
18 |
Correct |
58 ms |
20468 KB |
Output is correct |
19 |
Correct |
49 ms |
19032 KB |
Output is correct |
20 |
Correct |
39 ms |
17668 KB |
Output is correct |
21 |
Correct |
96 ms |
47176 KB |
Output is correct |
22 |
Correct |
118 ms |
45604 KB |
Output is correct |
23 |
Correct |
161 ms |
39560 KB |
Output is correct |
24 |
Correct |
101 ms |
45908 KB |
Output is correct |
25 |
Correct |
410 ms |
81024 KB |
Output is correct |
26 |
Correct |
298 ms |
111964 KB |
Output is correct |
27 |
Correct |
425 ms |
97144 KB |
Output is correct |
28 |
Correct |
502 ms |
94084 KB |
Output is correct |
29 |
Correct |
516 ms |
91908 KB |
Output is correct |
30 |
Correct |
496 ms |
93992 KB |
Output is correct |
31 |
Correct |
361 ms |
52052 KB |
Output is correct |
32 |
Correct |
343 ms |
96352 KB |
Output is correct |