#include <bits/stdc++.h>
using namespace std;
int n, m;
char a[4002][4002];
int dist[4002][4002];
int dx[4]={1,-1,0,0};
int dy[4]={0, 0,1,-1};
int isok(int x, int y){
return (0<x)&&(x<=n)&&(0<y)&&(y<=m)&&(dist[x][y]==0);
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;++i){
for(int j=1;j<=m;++j){
cin>>a[i][j];
}
}
deque<pair<int, int>> q;
q.push_back({1, 1});
dist[1][1]=1;
int sol=0;
while(!q.empty()){
int x, y;
tie(x, y)=q.front();
q.pop_front();
for(int k=0;k<4;++k){
int nx=x+dx[k], ny=y+dy[k];
if(isok(nx, ny)){
if(a[nx][ny]=='.') continue;
if(a[nx][ny]==a[x][y]){
dist[nx][ny]=dist[x][y];
sol=max(sol, dist[nx][ny]);
q.push_front({nx, ny});
}
else{
dist[nx][ny]=dist[x][y]+1;
sol=max(sol, dist[nx][ny]);
q.push_back({nx, ny});
}
}
}
}
cout<<sol<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
5316 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
716 KB |
Output is correct |
4 |
Correct |
16 ms |
5084 KB |
Output is correct |
5 |
Correct |
8 ms |
2864 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
716 KB |
Output is correct |
8 |
Correct |
1 ms |
748 KB |
Output is correct |
9 |
Correct |
2 ms |
1100 KB |
Output is correct |
10 |
Correct |
6 ms |
2380 KB |
Output is correct |
11 |
Correct |
5 ms |
2124 KB |
Output is correct |
12 |
Correct |
10 ms |
3020 KB |
Output is correct |
13 |
Correct |
8 ms |
3020 KB |
Output is correct |
14 |
Correct |
8 ms |
2872 KB |
Output is correct |
15 |
Correct |
24 ms |
5196 KB |
Output is correct |
16 |
Correct |
25 ms |
5364 KB |
Output is correct |
17 |
Correct |
24 ms |
5188 KB |
Output is correct |
18 |
Correct |
16 ms |
5128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
30796 KB |
Output is correct |
2 |
Correct |
128 ms |
15072 KB |
Output is correct |
3 |
Correct |
1116 ms |
65312 KB |
Output is correct |
4 |
Correct |
277 ms |
32636 KB |
Output is correct |
5 |
Correct |
690 ms |
49384 KB |
Output is correct |
6 |
Correct |
1473 ms |
99832 KB |
Output is correct |
7 |
Correct |
21 ms |
32128 KB |
Output is correct |
8 |
Correct |
22 ms |
30788 KB |
Output is correct |
9 |
Correct |
6 ms |
560 KB |
Output is correct |
10 |
Correct |
3 ms |
332 KB |
Output is correct |
11 |
Correct |
20 ms |
31604 KB |
Output is correct |
12 |
Correct |
3 ms |
1612 KB |
Output is correct |
13 |
Correct |
132 ms |
14936 KB |
Output is correct |
14 |
Correct |
74 ms |
10280 KB |
Output is correct |
15 |
Correct |
81 ms |
13012 KB |
Output is correct |
16 |
Correct |
54 ms |
5700 KB |
Output is correct |
17 |
Correct |
362 ms |
28872 KB |
Output is correct |
18 |
Correct |
323 ms |
35612 KB |
Output is correct |
19 |
Correct |
303 ms |
32604 KB |
Output is correct |
20 |
Correct |
246 ms |
25412 KB |
Output is correct |
21 |
Correct |
656 ms |
48616 KB |
Output is correct |
22 |
Correct |
667 ms |
49460 KB |
Output is correct |
23 |
Correct |
633 ms |
41156 KB |
Output is correct |
24 |
Correct |
668 ms |
45760 KB |
Output is correct |
25 |
Correct |
1309 ms |
86304 KB |
Output is correct |
26 |
Correct |
1006 ms |
123784 KB |
Output is correct |
27 |
Correct |
1316 ms |
113124 KB |
Output is correct |
28 |
Correct |
1430 ms |
99804 KB |
Output is correct |
29 |
Correct |
1420 ms |
98256 KB |
Output is correct |
30 |
Correct |
1352 ms |
102228 KB |
Output is correct |
31 |
Correct |
1008 ms |
68284 KB |
Output is correct |
32 |
Correct |
1269 ms |
97840 KB |
Output is correct |