#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() {
ios_base::sync_with_stdio(0);
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.empty()) {
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];
int 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 << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
3948 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
620 KB |
Output is correct |
4 |
Correct |
8 ms |
3564 KB |
Output is correct |
5 |
Correct |
3 ms |
2028 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 |
768 KB |
Output is correct |
9 |
Correct |
1 ms |
876 KB |
Output is correct |
10 |
Correct |
3 ms |
1772 KB |
Output is correct |
11 |
Correct |
3 ms |
1516 KB |
Output is correct |
12 |
Correct |
7 ms |
2156 KB |
Output is correct |
13 |
Correct |
3 ms |
2028 KB |
Output is correct |
14 |
Correct |
4 ms |
2048 KB |
Output is correct |
15 |
Correct |
15 ms |
3692 KB |
Output is correct |
16 |
Correct |
14 ms |
3948 KB |
Output is correct |
17 |
Correct |
10 ms |
3692 KB |
Output is correct |
18 |
Correct |
9 ms |
3564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
15852 KB |
Output is correct |
2 |
Correct |
45 ms |
12012 KB |
Output is correct |
3 |
Correct |
232 ms |
75464 KB |
Output is correct |
4 |
Correct |
76 ms |
29452 KB |
Output is correct |
5 |
Correct |
196 ms |
51324 KB |
Output is correct |
6 |
Correct |
687 ms |
109772 KB |
Output is correct |
7 |
Correct |
13 ms |
16620 KB |
Output is correct |
8 |
Correct |
13 ms |
15852 KB |
Output is correct |
9 |
Correct |
2 ms |
876 KB |
Output is correct |
10 |
Correct |
1 ms |
620 KB |
Output is correct |
11 |
Correct |
13 ms |
16364 KB |
Output is correct |
12 |
Correct |
2 ms |
1260 KB |
Output is correct |
13 |
Correct |
45 ms |
12012 KB |
Output is correct |
14 |
Correct |
26 ms |
7916 KB |
Output is correct |
15 |
Correct |
23 ms |
10348 KB |
Output is correct |
16 |
Correct |
21 ms |
4972 KB |
Output is correct |
17 |
Correct |
125 ms |
25324 KB |
Output is correct |
18 |
Correct |
94 ms |
32108 KB |
Output is correct |
19 |
Correct |
80 ms |
29420 KB |
Output is correct |
20 |
Correct |
61 ms |
22508 KB |
Output is correct |
21 |
Correct |
144 ms |
50796 KB |
Output is correct |
22 |
Correct |
197 ms |
51564 KB |
Output is correct |
23 |
Correct |
234 ms |
42220 KB |
Output is correct |
24 |
Correct |
144 ms |
47596 KB |
Output is correct |
25 |
Correct |
465 ms |
96364 KB |
Output is correct |
26 |
Correct |
429 ms |
130852 KB |
Output is correct |
27 |
Correct |
574 ms |
122984 KB |
Output is correct |
28 |
Correct |
685 ms |
109956 KB |
Output is correct |
29 |
Correct |
684 ms |
108436 KB |
Output is correct |
30 |
Correct |
665 ms |
112428 KB |
Output is correct |
31 |
Correct |
562 ms |
72172 KB |
Output is correct |
32 |
Correct |
494 ms |
108436 KB |
Output is correct |