#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;
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
10 ms |
1880 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Correct |
5 ms |
1620 KB |
Output is correct |
5 |
Incorrect |
3 ms |
860 KB |
Output isn't correct |
6 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
10 |
Incorrect |
2 ms |
860 KB |
Output isn't correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Incorrect |
3 ms |
1076 KB |
Output isn't correct |
13 |
Incorrect |
3 ms |
860 KB |
Output isn't correct |
14 |
Incorrect |
3 ms |
980 KB |
Output isn't correct |
15 |
Incorrect |
8 ms |
2136 KB |
Output isn't correct |
16 |
Incorrect |
9 ms |
1884 KB |
Output isn't correct |
17 |
Incorrect |
8 ms |
1884 KB |
Output isn't correct |
18 |
Correct |
6 ms |
1628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
860 KB |
Output isn't correct |
2 |
Incorrect |
43 ms |
13472 KB |
Output isn't correct |
3 |
Incorrect |
413 ms |
139464 KB |
Output isn't correct |
4 |
Incorrect |
103 ms |
23380 KB |
Output isn't correct |
5 |
Incorrect |
231 ms |
91584 KB |
Output isn't correct |
6 |
Correct |
494 ms |
109640 KB |
Output is correct |
7 |
Incorrect |
3 ms |
1116 KB |
Output isn't correct |
8 |
Incorrect |
2 ms |
860 KB |
Output isn't correct |
9 |
Incorrect |
2 ms |
860 KB |
Output isn't correct |
10 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
11 |
Incorrect |
2 ms |
860 KB |
Output isn't correct |
12 |
Incorrect |
1 ms |
504 KB |
Output isn't correct |
13 |
Incorrect |
41 ms |
13496 KB |
Output isn't correct |
14 |
Incorrect |
24 ms |
7948 KB |
Output isn't correct |
15 |
Incorrect |
29 ms |
8796 KB |
Output isn't correct |
16 |
Incorrect |
19 ms |
5604 KB |
Output isn't correct |
17 |
Incorrect |
112 ms |
34480 KB |
Output isn't correct |
18 |
Incorrect |
184 ms |
33508 KB |
Output isn't correct |
19 |
Incorrect |
105 ms |
23572 KB |
Output isn't correct |
20 |
Incorrect |
97 ms |
30236 KB |
Output isn't correct |
21 |
Incorrect |
231 ms |
77152 KB |
Output isn't correct |
22 |
Incorrect |
230 ms |
91620 KB |
Output isn't correct |
23 |
Incorrect |
222 ms |
65768 KB |
Output isn't correct |
24 |
Incorrect |
220 ms |
79944 KB |
Output isn't correct |
25 |
Incorrect |
838 ms |
134248 KB |
Output isn't correct |
26 |
Correct |
307 ms |
124084 KB |
Output is correct |
27 |
Correct |
388 ms |
112932 KB |
Output is correct |
28 |
Correct |
496 ms |
109672 KB |
Output is correct |
29 |
Correct |
525 ms |
107680 KB |
Output is correct |
30 |
Correct |
456 ms |
109212 KB |
Output is correct |
31 |
Incorrect |
406 ms |
62276 KB |
Output isn't correct |
32 |
Incorrect |
430 ms |
112324 KB |
Output isn't correct |