#include <bits/stdc++.h>
using namespace std;
#define int long long
#define MAXN 4002
int dx[4]{1, -1, 0, 0}, dy[4]{0, 0, 1, -1};
int n, m, depth[MAXN][MAXN], ans = 1;
char snow[MAXN][MAXN];
bool inside(int x, int y) {
return (x > -1 && x < n && y > -1 && y < m && snow[x][y] != '.');
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n>>m;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>snow[i][j];
}
}
memset(depth,0,sizeof(depth));
deque<pair<int,int>>dq;
dq.push_back({0,0});
depth[0][0]=1;
while(!dq.empty()){
auto curr = dq.front();
int r = curr.first, c = curr.second;
dq.pop_front();
ans = max(ans,depth[r][c]);
for(int i=0;i<4;i++){
int nr = r+dx[i], nc = c+dy[i];
if(inside(nr,nc)&&depth[nr][nc]==0){
if(snow[r][c]==snow[nr][nc]){
depth[nr][nc] = depth[r][c];
dq.push_front({nr,nc});
}
else{
depth[nr][nc] = depth[r][c]+1;
dq.push_back({nr,nc});
}
}
}
}
cout<<ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
129372 KB |
Output is correct |
2 |
Correct |
20 ms |
126836 KB |
Output is correct |
3 |
Correct |
17 ms |
129108 KB |
Output is correct |
4 |
Correct |
22 ms |
129616 KB |
Output is correct |
5 |
Correct |
19 ms |
129172 KB |
Output is correct |
6 |
Correct |
17 ms |
126816 KB |
Output is correct |
7 |
Correct |
17 ms |
128992 KB |
Output is correct |
8 |
Correct |
16 ms |
129116 KB |
Output is correct |
9 |
Correct |
18 ms |
129120 KB |
Output is correct |
10 |
Correct |
19 ms |
129116 KB |
Output is correct |
11 |
Correct |
19 ms |
129116 KB |
Output is correct |
12 |
Correct |
22 ms |
129216 KB |
Output is correct |
13 |
Correct |
19 ms |
129136 KB |
Output is correct |
14 |
Correct |
17 ms |
129124 KB |
Output is correct |
15 |
Correct |
26 ms |
129468 KB |
Output is correct |
16 |
Correct |
26 ms |
129372 KB |
Output is correct |
17 |
Correct |
27 ms |
129372 KB |
Output is correct |
18 |
Correct |
21 ms |
129624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
141484 KB |
Output is correct |
2 |
Correct |
52 ms |
134228 KB |
Output is correct |
3 |
Correct |
246 ms |
149732 KB |
Output is correct |
4 |
Correct |
82 ms |
137320 KB |
Output is correct |
5 |
Correct |
172 ms |
143956 KB |
Output is correct |
6 |
Correct |
556 ms |
177392 KB |
Output is correct |
7 |
Correct |
21 ms |
141404 KB |
Output is correct |
8 |
Correct |
20 ms |
141392 KB |
Output is correct |
9 |
Correct |
18 ms |
127072 KB |
Output is correct |
10 |
Correct |
19 ms |
126804 KB |
Output is correct |
11 |
Correct |
19 ms |
141404 KB |
Output is correct |
12 |
Correct |
20 ms |
129116 KB |
Output is correct |
13 |
Correct |
51 ms |
134228 KB |
Output is correct |
14 |
Correct |
37 ms |
132184 KB |
Output is correct |
15 |
Correct |
37 ms |
131932 KB |
Output is correct |
16 |
Correct |
33 ms |
129668 KB |
Output is correct |
17 |
Correct |
101 ms |
137556 KB |
Output is correct |
18 |
Correct |
95 ms |
137740 KB |
Output is correct |
19 |
Correct |
88 ms |
137264 KB |
Output is correct |
20 |
Correct |
71 ms |
137296 KB |
Output is correct |
21 |
Correct |
154 ms |
144236 KB |
Output is correct |
22 |
Correct |
177 ms |
143976 KB |
Output is correct |
23 |
Correct |
187 ms |
141392 KB |
Output is correct |
24 |
Correct |
148 ms |
144032 KB |
Output is correct |
25 |
Correct |
478 ms |
149668 KB |
Output is correct |
26 |
Correct |
349 ms |
247844 KB |
Output is correct |
27 |
Correct |
423 ms |
202852 KB |
Output is correct |
28 |
Correct |
558 ms |
176884 KB |
Output is correct |
29 |
Correct |
550 ms |
173492 KB |
Output is correct |
30 |
Correct |
510 ms |
185032 KB |
Output is correct |
31 |
Correct |
444 ms |
147712 KB |
Output is correct |
32 |
Correct |
395 ms |
180204 KB |
Output is correct |