#include<bits/stdc++.h>
using namespace std;
char a[4005][4005];
int dist[4005][4005],dx[4]={-1,0,0,1},dy[4]={0,-1,1,0};
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen("TEST.inp","r",stdin);
int n,m; cin>>n>>m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>a[i][j];
}
}
dist[1][1]=1;
deque<pair<int,int>>q;
int ans=1;
q.push_back({1,1});
while(!q.empty())
{
pair<int,int>x=q.front(); q.pop_front();
//cout<<x.first<<' '<<x.second<<' '<<dist[x.first][x.second]<<'\n';
ans=max(ans,dist[x.first][x.second]);
for(int k=0;k<4;k++)
{
int nx=x.first+dx[k],ny=x.second+dy[k];
if(nx>=1&&nx<=n&&ny>=1&&ny<=m&&dist[nx][ny]==0&&a[nx][ny]!='.')
{
if(a[x.first][x.second]==a[nx][ny])
{
dist[nx][ny]=dist[x.first][x.second];
q.push_front({nx,ny});
}
else
{
dist[nx][ny]=dist[x.first][x.second]+1;
q.push_back({nx,ny});
}
}
}
}
cout<<ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
5468 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
856 KB |
Output is correct |
4 |
Correct |
6 ms |
5232 KB |
Output is correct |
5 |
Correct |
3 ms |
2908 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
3 ms |
2652 KB |
Output is correct |
11 |
Correct |
2 ms |
2140 KB |
Output is correct |
12 |
Correct |
4 ms |
3080 KB |
Output is correct |
13 |
Correct |
3 ms |
2908 KB |
Output is correct |
14 |
Correct |
2 ms |
2908 KB |
Output is correct |
15 |
Correct |
9 ms |
5344 KB |
Output is correct |
16 |
Correct |
10 ms |
5600 KB |
Output is correct |
17 |
Correct |
8 ms |
5240 KB |
Output is correct |
18 |
Correct |
7 ms |
5212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
30812 KB |
Output is correct |
2 |
Correct |
44 ms |
15196 KB |
Output is correct |
3 |
Correct |
243 ms |
73468 KB |
Output is correct |
4 |
Correct |
70 ms |
32852 KB |
Output is correct |
5 |
Correct |
178 ms |
53332 KB |
Output is correct |
6 |
Correct |
505 ms |
109252 KB |
Output is correct |
7 |
Correct |
13 ms |
32348 KB |
Output is correct |
8 |
Correct |
13 ms |
30948 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
12 ms |
31576 KB |
Output is correct |
12 |
Correct |
1 ms |
1628 KB |
Output is correct |
13 |
Correct |
39 ms |
15176 KB |
Output is correct |
14 |
Correct |
23 ms |
10584 KB |
Output is correct |
15 |
Correct |
20 ms |
13148 KB |
Output is correct |
16 |
Correct |
16 ms |
5728 KB |
Output is correct |
17 |
Correct |
100 ms |
28844 KB |
Output is correct |
18 |
Correct |
71 ms |
35664 KB |
Output is correct |
19 |
Correct |
66 ms |
32848 KB |
Output is correct |
20 |
Correct |
53 ms |
25424 KB |
Output is correct |
21 |
Correct |
137 ms |
52560 KB |
Output is correct |
22 |
Correct |
157 ms |
53328 KB |
Output is correct |
23 |
Correct |
172 ms |
43856 KB |
Output is correct |
24 |
Correct |
136 ms |
49744 KB |
Output is correct |
25 |
Correct |
294 ms |
94440 KB |
Output is correct |
26 |
Correct |
277 ms |
131164 KB |
Output is correct |
27 |
Correct |
386 ms |
134872 KB |
Output is correct |
28 |
Correct |
470 ms |
109368 KB |
Output is correct |
29 |
Correct |
485 ms |
106436 KB |
Output is correct |
30 |
Correct |
459 ms |
109700 KB |
Output is correct |
31 |
Correct |
354 ms |
73512 KB |
Output is correct |
32 |
Correct |
351 ms |
116776 KB |
Output is correct |