#include <bits/stdc++.h>
using namespace std;
const int N=1050;
char base[N][N];
int wd[N][N],U[N][N],D[N][N],L[N][N],R[N][N];
int mv[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
int dist[N][N];
int main(){
int n,m,sx,sy,ex,ey;
scanf("%i %i",&n,&m);
for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)wd[i][j]=min({i,j,n-i+1,m-j+1});
queue<int> q;
for(int i=1;i<=n;i++){
scanf("%s",base[i]+1);
for(int j=1;j<=m;j++){
if(base[i][j]=='S'){
sx=i,sy=j;
base[i][j]='.';
}else if(base[i][j]=='C'){
ex=i,ey=j;
base[i][j]='.';
}else if(base[i][j]=='#'){
wd[i][j]=0;
q.push(i*N+j);
}
}
}
while(q.size()){
int x=q.front()/N;
int y=q.front()%N;
q.pop();
for(int k=0;k<4;k++){
int i=x+mv[k][0];
int j=y+mv[k][1];
if(base[i][j]=='.'){
if(wd[i][j]>wd[x][y]+1){
wd[i][j]=wd[x][y]+1;
q.push(i*N+j);
}
}
}
}
for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)U[i][j]=base[i-1][j]=='.'?U[i-1][j]:i*N+j,L[i][j]=base[i][j-1]=='.'?L[i][j-1]:i*N+j;
for(int i=n;i>=1;i--)for(int j=m;j>=1;j--)D[i][j]=base[i+1][j]=='.'?D[i+1][j]:i*N+j,R[i][j]=base[i][j+1]=='.'?R[i][j+1]:i*N+j;
for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)dist[i][j]=1e9;
dist[sx][sy]=0;
priority_queue<pair<int,int>> pq;
pq.push({-dist[sx][sy],sx*N+sy});
auto Try=[&](int x,int y,int d){
if(dist[x][y]>d){
dist[x][y]=d;
pq.push({-dist[x][y],x*N+y});
}
};
while(pq.size()){
int x=pq.top().second/N;
int y=pq.top().second%N;
int d=-pq.top().first;
pq.pop();
if(d!=dist[x][y])continue;
for(int k=0;k<4;k++){
int i=x+mv[k][0];
int j=y+mv[k][1];
if(base[i][j]=='.'){
Try(i,j,dist[x][y]+1);
}
}
Try(U[x][y]/N,U[x][y]%N,dist[x][y]+wd[x][y]);
Try(D[x][y]/N,D[x][y]%N,dist[x][y]+wd[x][y]);
Try(L[x][y]/N,L[x][y]%N,dist[x][y]+wd[x][y]);
Try(R[x][y]/N,R[x][y]%N,dist[x][y]+wd[x][y]);
}
printf("%i\n",dist[ex][ey]);
return 0;
}
Compilation message
portals.cpp: In function 'int main()':
portals.cpp:10:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i %i",&n,&m);
~~~~~^~~~~~~~~~~~~~~
portals.cpp:14:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s",base[i]+1);
~~~~~^~~~~~~~~~~~~~~~
portals.cpp:73:8: warning: 'ey' may be used uninitialized in this function [-Wmaybe-uninitialized]
printf("%i\n",dist[ex][ey]);
~~~~~~^~~~~~~~~~~~~~~~~~~~~
portals.cpp:73:8: warning: 'ex' may be used uninitialized in this function [-Wmaybe-uninitialized]
portals.cpp:48:29: warning: 'sy' may be used uninitialized in this function [-Wmaybe-uninitialized]
pq.push({-dist[sx][sy],sx*N+sy});
~~~~^~~
portals.cpp:48:27: warning: 'sx' may be used uninitialized in this function [-Wmaybe-uninitialized]
pq.push({-dist[sx][sy],sx*N+sy});
~~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
632 KB |
Output is correct |
3 |
Correct |
5 ms |
632 KB |
Output is correct |
4 |
Correct |
6 ms |
508 KB |
Output is correct |
5 |
Correct |
6 ms |
612 KB |
Output is correct |
6 |
Correct |
5 ms |
636 KB |
Output is correct |
7 |
Correct |
5 ms |
632 KB |
Output is correct |
8 |
Correct |
5 ms |
636 KB |
Output is correct |
9 |
Correct |
5 ms |
504 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
504 KB |
Output is correct |
3 |
Correct |
5 ms |
632 KB |
Output is correct |
4 |
Correct |
6 ms |
504 KB |
Output is correct |
5 |
Correct |
6 ms |
632 KB |
Output is correct |
6 |
Correct |
6 ms |
632 KB |
Output is correct |
7 |
Correct |
5 ms |
632 KB |
Output is correct |
8 |
Correct |
6 ms |
632 KB |
Output is correct |
9 |
Correct |
6 ms |
1656 KB |
Output is correct |
10 |
Correct |
6 ms |
1660 KB |
Output is correct |
11 |
Correct |
6 ms |
1656 KB |
Output is correct |
12 |
Correct |
6 ms |
1660 KB |
Output is correct |
13 |
Correct |
6 ms |
1656 KB |
Output is correct |
14 |
Correct |
5 ms |
508 KB |
Output is correct |
15 |
Correct |
7 ms |
1656 KB |
Output is correct |
16 |
Correct |
5 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
380 KB |
Output is correct |
2 |
Correct |
6 ms |
632 KB |
Output is correct |
3 |
Correct |
5 ms |
632 KB |
Output is correct |
4 |
Correct |
5 ms |
504 KB |
Output is correct |
5 |
Correct |
18 ms |
5624 KB |
Output is correct |
6 |
Correct |
14 ms |
5624 KB |
Output is correct |
7 |
Correct |
23 ms |
5624 KB |
Output is correct |
8 |
Correct |
10 ms |
5624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
632 KB |
Output is correct |
3 |
Correct |
5 ms |
632 KB |
Output is correct |
4 |
Correct |
5 ms |
504 KB |
Output is correct |
5 |
Correct |
5 ms |
636 KB |
Output is correct |
6 |
Correct |
5 ms |
632 KB |
Output is correct |
7 |
Correct |
6 ms |
632 KB |
Output is correct |
8 |
Correct |
6 ms |
632 KB |
Output is correct |
9 |
Correct |
7 ms |
1656 KB |
Output is correct |
10 |
Correct |
7 ms |
1656 KB |
Output is correct |
11 |
Correct |
8 ms |
1660 KB |
Output is correct |
12 |
Correct |
7 ms |
1656 KB |
Output is correct |
13 |
Correct |
13 ms |
1656 KB |
Output is correct |
14 |
Correct |
14 ms |
5624 KB |
Output is correct |
15 |
Correct |
14 ms |
5624 KB |
Output is correct |
16 |
Correct |
14 ms |
5628 KB |
Output is correct |
17 |
Correct |
14 ms |
5624 KB |
Output is correct |
18 |
Correct |
14 ms |
5624 KB |
Output is correct |
19 |
Correct |
14 ms |
5496 KB |
Output is correct |
20 |
Correct |
14 ms |
5496 KB |
Output is correct |
21 |
Correct |
14 ms |
5624 KB |
Output is correct |
22 |
Correct |
15 ms |
5624 KB |
Output is correct |
23 |
Correct |
14 ms |
5624 KB |
Output is correct |
24 |
Correct |
14 ms |
5496 KB |
Output is correct |
25 |
Correct |
5 ms |
504 KB |
Output is correct |
26 |
Correct |
6 ms |
1656 KB |
Output is correct |
27 |
Correct |
9 ms |
504 KB |
Output is correct |
28 |
Correct |
9 ms |
5624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
632 KB |
Output is correct |
3 |
Correct |
6 ms |
632 KB |
Output is correct |
4 |
Correct |
5 ms |
504 KB |
Output is correct |
5 |
Correct |
5 ms |
632 KB |
Output is correct |
6 |
Correct |
6 ms |
632 KB |
Output is correct |
7 |
Correct |
5 ms |
632 KB |
Output is correct |
8 |
Correct |
5 ms |
632 KB |
Output is correct |
9 |
Correct |
6 ms |
1656 KB |
Output is correct |
10 |
Correct |
8 ms |
1784 KB |
Output is correct |
11 |
Correct |
6 ms |
1656 KB |
Output is correct |
12 |
Correct |
7 ms |
1656 KB |
Output is correct |
13 |
Correct |
6 ms |
1656 KB |
Output is correct |
14 |
Correct |
14 ms |
5624 KB |
Output is correct |
15 |
Correct |
28 ms |
5624 KB |
Output is correct |
16 |
Correct |
15 ms |
5624 KB |
Output is correct |
17 |
Correct |
14 ms |
5624 KB |
Output is correct |
18 |
Correct |
15 ms |
5624 KB |
Output is correct |
19 |
Correct |
16 ms |
5496 KB |
Output is correct |
20 |
Correct |
13 ms |
5496 KB |
Output is correct |
21 |
Correct |
14 ms |
5624 KB |
Output is correct |
22 |
Correct |
14 ms |
5628 KB |
Output is correct |
23 |
Correct |
15 ms |
5628 KB |
Output is correct |
24 |
Correct |
193 ms |
28696 KB |
Output is correct |
25 |
Correct |
387 ms |
27256 KB |
Output is correct |
26 |
Correct |
240 ms |
27300 KB |
Output is correct |
27 |
Correct |
228 ms |
27256 KB |
Output is correct |
28 |
Correct |
168 ms |
29164 KB |
Output is correct |
29 |
Correct |
202 ms |
28960 KB |
Output is correct |
30 |
Correct |
238 ms |
29004 KB |
Output is correct |
31 |
Correct |
17 ms |
5528 KB |
Output is correct |
32 |
Correct |
207 ms |
27104 KB |
Output is correct |
33 |
Correct |
5 ms |
504 KB |
Output is correct |
34 |
Correct |
6 ms |
1656 KB |
Output is correct |
35 |
Correct |
174 ms |
28152 KB |
Output is correct |
36 |
Correct |
5 ms |
376 KB |
Output is correct |
37 |
Correct |
10 ms |
5624 KB |
Output is correct |
38 |
Correct |
89 ms |
28756 KB |
Output is correct |
39 |
Correct |
120 ms |
29048 KB |
Output is correct |