#include <bits/stdc++.h>
using namespace std;
int dx[4]{1, -1, 0, 0}, dy[4]{0, 0, 1, -1};
int n, m, depth[4000][4000], ans = 1;
string snow[4000];
bool inside(int x, int y) {
return (x > -1 && x < n && y > -1 && y < m && snow[x][y] != '.');
}
int main() {
iostream::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> snow[i];
deque<pair<int, int>> q;
q.push_back({0, 0});
depth[0][0] = 1;
while (q.size()) {
pair<int, int> c = q.front();
q.pop_front();
ans = max(ans, depth[c.first][c.second]);
for (int i = 0; i < 4; i++) {
int x = c.first + dx[i], y = c.second + dy[i];
if (inside(x, y) && depth[x][y] == 0) {
if (snow[x][y] == snow[c.first][c.second]) {
depth[x][y] = depth[c.first][c.second];
q.push_front({x, y});
} else {
depth[x][y] = depth[c.first][c.second] + 1;
q.push_back({x, y});
}
}
}
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
3604 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
660 KB |
Output is correct |
4 |
Correct |
5 ms |
3296 KB |
Output is correct |
5 |
Correct |
3 ms |
1876 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
724 KB |
Output is correct |
9 |
Correct |
1 ms |
852 KB |
Output is correct |
10 |
Correct |
2 ms |
1620 KB |
Output is correct |
11 |
Correct |
3 ms |
1492 KB |
Output is correct |
12 |
Correct |
5 ms |
2004 KB |
Output is correct |
13 |
Correct |
3 ms |
1876 KB |
Output is correct |
14 |
Correct |
3 ms |
1876 KB |
Output is correct |
15 |
Correct |
9 ms |
3412 KB |
Output is correct |
16 |
Correct |
11 ms |
3540 KB |
Output is correct |
17 |
Correct |
9 ms |
3544 KB |
Output is correct |
18 |
Correct |
6 ms |
3336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
15844 KB |
Output is correct |
2 |
Correct |
41 ms |
10244 KB |
Output is correct |
3 |
Correct |
163 ms |
59812 KB |
Output is correct |
4 |
Correct |
59 ms |
25712 KB |
Output is correct |
5 |
Correct |
145 ms |
42844 KB |
Output is correct |
6 |
Correct |
485 ms |
94360 KB |
Output is correct |
7 |
Correct |
10 ms |
16468 KB |
Output is correct |
8 |
Correct |
11 ms |
15844 KB |
Output is correct |
9 |
Correct |
2 ms |
724 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
11 ms |
16248 KB |
Output is correct |
12 |
Correct |
1 ms |
1108 KB |
Output is correct |
13 |
Correct |
34 ms |
10264 KB |
Output is correct |
14 |
Correct |
20 ms |
6996 KB |
Output is correct |
15 |
Correct |
15 ms |
9300 KB |
Output is correct |
16 |
Correct |
16 ms |
4348 KB |
Output is correct |
17 |
Correct |
104 ms |
21292 KB |
Output is correct |
18 |
Correct |
61 ms |
28180 KB |
Output is correct |
19 |
Correct |
57 ms |
25648 KB |
Output is correct |
20 |
Correct |
43 ms |
18996 KB |
Output is correct |
21 |
Correct |
107 ms |
41388 KB |
Output is correct |
22 |
Correct |
128 ms |
42848 KB |
Output is correct |
23 |
Correct |
159 ms |
34628 KB |
Output is correct |
24 |
Correct |
101 ms |
38632 KB |
Output is correct |
25 |
Correct |
344 ms |
80700 KB |
Output is correct |
26 |
Correct |
327 ms |
118680 KB |
Output is correct |
27 |
Correct |
395 ms |
107328 KB |
Output is correct |
28 |
Correct |
497 ms |
94276 KB |
Output is correct |
29 |
Correct |
502 ms |
92744 KB |
Output is correct |
30 |
Correct |
436 ms |
96996 KB |
Output is correct |
31 |
Correct |
415 ms |
62008 KB |
Output is correct |
32 |
Correct |
348 ms |
92608 KB |
Output is correct |