#include <bits/stdc++.h>
using namespace std;
#define sz(x) (int)(x).size()
const int MAXN = 4005;
const int dx[4] = {-1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
int n, m;
char snow[MAXN][MAXN];
int depth[MAXN][MAXN];
deque<pair<int, int>> dq;
bool inbounds(int x, int y){
return (x >= 0 && y >= 0 && x < n && y < m && snow[x][y] != '.');
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
cin >> n >> m;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
cin >> snow[i][j];
}
}
depth[0][0] = 1;
dq.push_back(make_pair(0, 0));
int ans = 0;
while(!dq.empty()){
pair<int, int> curr = dq.front();
dq.pop_front();
ans = max(ans, depth[curr.first][curr.second]);
for(int i = 0; i < 4; i++){
int x = curr.first + dx[i];
int y = curr.second + dy[i];
if(inbounds(x, y) && !depth[x][y]){
if(snow[x][y] == snow[curr.first][curr.second]){
depth[x][y] = depth[curr.first][curr.second];
dq.push_front(make_pair(x, y));
}
else{
depth[x][y] = depth[curr.first][curr.second] + 1;
dq.push_back(make_pair(x, y));
}
}
}
}
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
5204 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
6 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
2900 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
724 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
3 ms |
2388 KB |
Output is correct |
11 |
Correct |
2 ms |
2132 KB |
Output is correct |
12 |
Correct |
6 ms |
2900 KB |
Output is correct |
13 |
Correct |
3 ms |
2896 KB |
Output is correct |
14 |
Correct |
3 ms |
2900 KB |
Output is correct |
15 |
Correct |
9 ms |
5076 KB |
Output is correct |
16 |
Correct |
11 ms |
5204 KB |
Output is correct |
17 |
Correct |
8 ms |
4948 KB |
Output is correct |
18 |
Correct |
6 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
30736 KB |
Output is correct |
2 |
Correct |
40 ms |
14196 KB |
Output is correct |
3 |
Correct |
254 ms |
71284 KB |
Output is correct |
4 |
Correct |
82 ms |
31528 KB |
Output is correct |
5 |
Correct |
184 ms |
51700 KB |
Output is correct |
6 |
Correct |
504 ms |
105284 KB |
Output is correct |
7 |
Correct |
15 ms |
32156 KB |
Output is correct |
8 |
Correct |
14 ms |
30804 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
14 ms |
31520 KB |
Output is correct |
12 |
Correct |
1 ms |
1492 KB |
Output is correct |
13 |
Correct |
40 ms |
14124 KB |
Output is correct |
14 |
Correct |
23 ms |
9596 KB |
Output is correct |
15 |
Correct |
22 ms |
12292 KB |
Output is correct |
16 |
Correct |
17 ms |
5076 KB |
Output is correct |
17 |
Correct |
101 ms |
27572 KB |
Output is correct |
18 |
Correct |
88 ms |
34468 KB |
Output is correct |
19 |
Correct |
84 ms |
31484 KB |
Output is correct |
20 |
Correct |
61 ms |
24392 KB |
Output is correct |
21 |
Correct |
149 ms |
50832 KB |
Output is correct |
22 |
Correct |
181 ms |
51628 KB |
Output is correct |
23 |
Correct |
187 ms |
42024 KB |
Output is correct |
24 |
Correct |
151 ms |
48080 KB |
Output is correct |
25 |
Correct |
423 ms |
92248 KB |
Output is correct |
26 |
Correct |
309 ms |
129060 KB |
Output is correct |
27 |
Correct |
395 ms |
109668 KB |
Output is correct |
28 |
Correct |
490 ms |
105440 KB |
Output is correct |
29 |
Correct |
500 ms |
105624 KB |
Output is correct |
30 |
Correct |
485 ms |
104872 KB |
Output is correct |
31 |
Correct |
405 ms |
71652 KB |
Output is correct |
32 |
Correct |
408 ms |
106912 KB |
Output is correct |