#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<pair<int, int>> vii;
typedef pair<int, int> pi;
#define MOD 1000000007LL
int H, W;
string grid[4000];
int depth[4000][4000];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
bool ins(int x, int y) {
return x >= 0 && y >= 0 && x < H && y < W && grid[x][y] != '.';
}
int main() {
cin >> H >> W;
for (int i = 0; i < H; i++) cin >> grid[i];
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++) depth[i][j] = 1e9;
depth[0][0] = 1;
int ans = 1;
deque<pair<int, int>> q; q.push_front({0, 0});
while (!q.empty()) {
int vr = q.front().first, vc = q.front().second; q.pop_front();
ans = max(ans, depth[vr][vc]);
for (int i = 0; i < 4; i++) {
int nr = vr + dx[i], nc = vc + dy[i];
if (ins(nr, nc) == false) continue;
int w = grid[vr][vc] != grid[nr][nc];
if (depth[vr][vc]+w < depth[nr][nc]) {
depth[nr][nc] = depth[vr][vc]+w;
if (w == 0) q.push_front({nr, nc});
else q.push_back({nr, nc});
}
}
//cout << "Finished rep for " << vr << " " << vc << " " << depth[vr][vc] << endl;
}
cout << ans << endl;
/*cout << "Debugging depth" << endl;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cout << depth[i][j] << " ";
}
cout << endl;
}*/
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
4076 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
620 KB |
Output is correct |
4 |
Correct |
12 ms |
3564 KB |
Output is correct |
5 |
Correct |
5 ms |
2284 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
620 KB |
Output is correct |
8 |
Correct |
1 ms |
748 KB |
Output is correct |
9 |
Correct |
1 ms |
876 KB |
Output is correct |
10 |
Correct |
4 ms |
1900 KB |
Output is correct |
11 |
Correct |
4 ms |
1516 KB |
Output is correct |
12 |
Correct |
8 ms |
2156 KB |
Output is correct |
13 |
Correct |
5 ms |
2156 KB |
Output is correct |
14 |
Correct |
5 ms |
2156 KB |
Output is correct |
15 |
Correct |
17 ms |
4204 KB |
Output is correct |
16 |
Correct |
19 ms |
4076 KB |
Output is correct |
17 |
Correct |
14 ms |
3948 KB |
Output is correct |
18 |
Correct |
13 ms |
3564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
15852 KB |
Output is correct |
2 |
Correct |
79 ms |
15468 KB |
Output is correct |
3 |
Correct |
567 ms |
108800 KB |
Output is correct |
4 |
Correct |
152 ms |
33388 KB |
Output is correct |
5 |
Correct |
359 ms |
67820 KB |
Output is correct |
6 |
Correct |
1167 ms |
123104 KB |
Output is correct |
7 |
Correct |
11 ms |
16620 KB |
Output is correct |
8 |
Correct |
10 ms |
15980 KB |
Output is correct |
9 |
Correct |
4 ms |
876 KB |
Output is correct |
10 |
Correct |
2 ms |
620 KB |
Output is correct |
11 |
Correct |
10 ms |
16364 KB |
Output is correct |
12 |
Correct |
2 ms |
1132 KB |
Output is correct |
13 |
Correct |
79 ms |
15468 KB |
Output is correct |
14 |
Correct |
45 ms |
10344 KB |
Output is correct |
15 |
Correct |
43 ms |
11216 KB |
Output is correct |
16 |
Correct |
35 ms |
5996 KB |
Output is correct |
17 |
Correct |
218 ms |
36176 KB |
Output is correct |
18 |
Correct |
168 ms |
35436 KB |
Output is correct |
19 |
Correct |
150 ms |
33388 KB |
Output is correct |
20 |
Correct |
129 ms |
31084 KB |
Output is correct |
21 |
Correct |
340 ms |
69740 KB |
Output is correct |
22 |
Correct |
355 ms |
67692 KB |
Output is correct |
23 |
Correct |
398 ms |
56684 KB |
Output is correct |
24 |
Correct |
332 ms |
69228 KB |
Output is correct |
25 |
Correct |
789 ms |
108908 KB |
Output is correct |
26 |
Correct |
774 ms |
130684 KB |
Output is correct |
27 |
Correct |
1049 ms |
143720 KB |
Output is correct |
28 |
Correct |
1155 ms |
122976 KB |
Output is correct |
29 |
Correct |
1146 ms |
120780 KB |
Output is correct |
30 |
Correct |
1085 ms |
126080 KB |
Output is correct |
31 |
Correct |
816 ms |
73196 KB |
Output is correct |
32 |
Correct |
948 ms |
133216 KB |
Output is correct |