#include <bits/stdc++.h>
using namespace std;
const int H = 4005;
const int dI[4] = {-1, 0, +1, 0};
const int dJ[4] = {0, +1, 0, -1};
int h, w;
char d[H][H];
int f[H][H];
bool prc[H][H];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> h >> w;
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
cin >> d[i][j];
}
}
deque<pair<int, int>> dq;
memset(f, 0x3f3f3f3f, sizeof(f));
f[h][w] = 1;
int mx = 1;
dq.push_front({h, w});
while (!dq.empty()) {
int x, y;
tie(x, y) = dq.front();
dq.pop_front();
if (prc[x][y]) {
continue;
}
mx = max(mx, f[x][y]);
prc[x][y] = true;
for (int dir = 0; dir < 4; dir++) {
int nx = x + dI[dir];
int ny = y + dJ[dir];
if (1 <= nx && nx <= h && 1 <= ny && ny <= w) {
if (d[nx][ny] != '.') {
if (d[x][y] == d[nx][ny]) {
if (f[nx][ny] > f[x][y]) {
f[nx][ny] = f[x][y];
dq.push_front({nx, ny});
}
} else if (f[nx][ny] > f[x][y] + 1) {
f[nx][ny] = f[x][y] + 1;
dq.push_back({nx, ny});
}
}
}
}
}
cout << mx;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
66900 KB |
Output is correct |
2 |
Correct |
20 ms |
63164 KB |
Output is correct |
3 |
Correct |
20 ms |
63476 KB |
Output is correct |
4 |
Correct |
28 ms |
67144 KB |
Output is correct |
5 |
Correct |
22 ms |
65328 KB |
Output is correct |
6 |
Correct |
21 ms |
63160 KB |
Output is correct |
7 |
Correct |
20 ms |
63496 KB |
Output is correct |
8 |
Correct |
21 ms |
63484 KB |
Output is correct |
9 |
Correct |
20 ms |
63828 KB |
Output is correct |
10 |
Correct |
23 ms |
65040 KB |
Output is correct |
11 |
Correct |
22 ms |
64712 KB |
Output is correct |
12 |
Correct |
26 ms |
65416 KB |
Output is correct |
13 |
Correct |
24 ms |
65356 KB |
Output is correct |
14 |
Correct |
23 ms |
65340 KB |
Output is correct |
15 |
Correct |
32 ms |
66964 KB |
Output is correct |
16 |
Correct |
36 ms |
66904 KB |
Output is correct |
17 |
Correct |
29 ms |
66872 KB |
Output is correct |
18 |
Correct |
29 ms |
67176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
92944 KB |
Output is correct |
2 |
Correct |
66 ms |
73568 KB |
Output is correct |
3 |
Correct |
290 ms |
101596 KB |
Output is correct |
4 |
Correct |
93 ms |
80388 KB |
Output is correct |
5 |
Correct |
168 ms |
93700 KB |
Output is correct |
6 |
Correct |
696 ms |
114108 KB |
Output is correct |
7 |
Correct |
32 ms |
94412 KB |
Output is correct |
8 |
Correct |
32 ms |
92972 KB |
Output is correct |
9 |
Correct |
21 ms |
63184 KB |
Output is correct |
10 |
Correct |
21 ms |
63160 KB |
Output is correct |
11 |
Correct |
33 ms |
93772 KB |
Output is correct |
12 |
Correct |
21 ms |
64228 KB |
Output is correct |
13 |
Correct |
65 ms |
73564 KB |
Output is correct |
14 |
Correct |
46 ms |
70280 KB |
Output is correct |
15 |
Correct |
42 ms |
71064 KB |
Output is correct |
16 |
Correct |
41 ms |
66252 KB |
Output is correct |
17 |
Correct |
131 ms |
81648 KB |
Output is correct |
18 |
Correct |
108 ms |
81572 KB |
Output is correct |
19 |
Correct |
96 ms |
80396 KB |
Output is correct |
20 |
Correct |
84 ms |
79080 KB |
Output is correct |
21 |
Correct |
186 ms |
94864 KB |
Output is correct |
22 |
Correct |
187 ms |
93900 KB |
Output is correct |
23 |
Correct |
270 ms |
88476 KB |
Output is correct |
24 |
Correct |
170 ms |
94728 KB |
Output is correct |
25 |
Correct |
478 ms |
101208 KB |
Output is correct |
26 |
Correct |
407 ms |
148440 KB |
Output is correct |
27 |
Correct |
545 ms |
130096 KB |
Output is correct |
28 |
Correct |
690 ms |
113952 KB |
Output is correct |
29 |
Correct |
707 ms |
112156 KB |
Output is correct |
30 |
Correct |
634 ms |
120520 KB |
Output is correct |
31 |
Correct |
541 ms |
96824 KB |
Output is correct |
32 |
Correct |
542 ms |
125524 KB |
Output is correct |