#include <bits/stdc++.h>
using namespace std;
const int N=4050;
const int inf=1e9;
int n,m,dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
char s[N][N];
int dist[N][N];
bool valid(int x,int y){
return x>=1&&x<=n&&y>=1&&y<=m&&s[x][y]!='.';
}
int main(){
scanf("%i%i",&n,&m);
for(int i=1;i<=n;i++)scanf("%s",s[i]+1);
for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)dist[i][j]=inf;
deque<pair<int,int>> q;
q.push_back({1,1});
dist[1][1]=1;
while(!q.empty()){
int x=q[0].first;
int y=q[0].second;
q.pop_front();
for(int dir=0;dir<4;dir++){
int nx=x+dx[dir],ny=y+dy[dir];
int w=(s[x][y]!=s[nx][ny]);
if(valid(nx,ny)&&dist[nx][ny]>dist[x][y]+w){
dist[nx][ny]=dist[x][y]+w;
if(w==0)q.push_front({nx,ny});
else q.push_back({nx,ny});
}
}
}
int ans=0;
for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)if(dist[i][j]!=inf)ans=max(ans,dist[i][j]);
printf("%i",ans);
return 0;
}
Compilation message
tracks.cpp: In function 'int main()':
tracks.cpp:12:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
12 | scanf("%i%i",&n,&m);
| ~~~~~^~~~~~~~~~~~~~
tracks.cpp:13:31: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
13 | for(int i=1;i<=n;i++)scanf("%s",s[i]+1);
| ~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
5324 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
716 KB |
Output is correct |
4 |
Correct |
8 ms |
5172 KB |
Output is correct |
5 |
Correct |
3 ms |
3000 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
692 KB |
Output is correct |
8 |
Correct |
1 ms |
716 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
3 ms |
2508 KB |
Output is correct |
11 |
Correct |
3 ms |
2124 KB |
Output is correct |
12 |
Correct |
5 ms |
3020 KB |
Output is correct |
13 |
Correct |
3 ms |
3020 KB |
Output is correct |
14 |
Correct |
3 ms |
3020 KB |
Output is correct |
15 |
Correct |
11 ms |
5324 KB |
Output is correct |
16 |
Correct |
13 ms |
5352 KB |
Output is correct |
17 |
Correct |
12 ms |
5068 KB |
Output is correct |
18 |
Correct |
8 ms |
5068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
31076 KB |
Output is correct |
2 |
Correct |
44 ms |
16780 KB |
Output is correct |
3 |
Correct |
250 ms |
80068 KB |
Output is correct |
4 |
Correct |
77 ms |
30276 KB |
Output is correct |
5 |
Correct |
189 ms |
59744 KB |
Output is correct |
6 |
Correct |
656 ms |
93924 KB |
Output is correct |
7 |
Correct |
15 ms |
32460 KB |
Output is correct |
8 |
Correct |
14 ms |
31052 KB |
Output is correct |
9 |
Correct |
2 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
14 ms |
31820 KB |
Output is correct |
12 |
Correct |
1 ms |
1612 KB |
Output is correct |
13 |
Correct |
53 ms |
16716 KB |
Output is correct |
14 |
Correct |
27 ms |
11328 KB |
Output is correct |
15 |
Correct |
27 ms |
12364 KB |
Output is correct |
16 |
Correct |
20 ms |
6148 KB |
Output is correct |
17 |
Correct |
111 ms |
32580 KB |
Output is correct |
18 |
Correct |
94 ms |
32164 KB |
Output is correct |
19 |
Correct |
78 ms |
30252 KB |
Output is correct |
20 |
Correct |
59 ms |
27948 KB |
Output is correct |
21 |
Correct |
147 ms |
61612 KB |
Output is correct |
22 |
Correct |
188 ms |
59588 KB |
Output is correct |
23 |
Correct |
220 ms |
50152 KB |
Output is correct |
24 |
Correct |
148 ms |
60984 KB |
Output is correct |
25 |
Correct |
493 ms |
79924 KB |
Output is correct |
26 |
Correct |
397 ms |
120224 KB |
Output is correct |
27 |
Correct |
543 ms |
114496 KB |
Output is correct |
28 |
Correct |
673 ms |
94068 KB |
Output is correct |
29 |
Correct |
658 ms |
91496 KB |
Output is correct |
30 |
Correct |
597 ms |
97248 KB |
Output is correct |
31 |
Correct |
500 ms |
64200 KB |
Output is correct |
32 |
Correct |
487 ms |
104132 KB |
Output is correct |