#include <bits/stdc++.h>
#define vc first
#define vp second
#define all(X) X.begin(),X.end()
using namespace std;
typedef pair<int,int> pii;
const int mx=1005, inf=1e8;
int s,e;
int R,C;
int D[mx*mx];
vector<int> rs[mx], cs[mx], rrs[mx], rcs[mx];
char buf[mx][mx+8];
void input(){
scanf("%d %d\n",&R,&C);
for(int i=1;i<=R;i++) scanf("%s",buf[i]+1);
for(int i=0;i<=C+1;i++) buf[0][i]=buf[R+1][i]='#';
for(int j=0;j<=R+1;j++) buf[j][0]=buf[j][C+1]='#';
for(int i=0;i<R+2;i++){
for(int j=0;j<C+2;j++){
if(buf[i][j]=='S') s=i*(C+2)+j;
else if(buf[i][j]=='C') e=i*(C+2)+j;
else if(buf[i][j]=='#'){
rs[i].push_back(j);
cs[j].push_back(i);
}
}
}
for(int i=0;i<R+2;i++){
rrs[i]=rs[i];
reverse(all(rrs[i]));
}
for(int j=0;j<C+2;j++){
rcs[j]=cs[j];
reverse(all(rcs[j]));
}
/*
for(int i=0;i<R+2;i++,puts("")) for(int k : rs[i]) printf("%d ",k);
for(int j=0;j<C+2;j++,puts("")) for(int k : cs[j]) printf("%d ",k);
for(int i=0;i<=R+1;i++) puts(buf[i]);
*/
fill(D,D+(R+2)*(C+2),inf);
R+=2, C+=2;
}
void dijk(){
priority_queue<pii,vector<pii>,greater<pii>> q;
int dr[4]={1,-1,C,-C};
q.push(pii(D[s]=0,s));
while(!q.empty()){
pii u = q.top(); q.pop();
int flag=0,tr=u.vp/C,tc=u.vp%C;
if(buf[tr][tc]=='#' || D[u.vp]<u.vc) continue;
//printf("(%d,%d) arrived\n",tr,tc);
for(int t=0;t<4;t++){
int np=u.vp+dr[t];
if(buf[np/C][np%C]=='#') flag=1;
else{
if(D[np]>u.vc+1) q.push(pii(D[np]=u.vc+1,np));
}
}
int nr[4]={tr,tr,*lower_bound(all(cs[tc]),tr)-1,*lower_bound(all(rcs[tc]),tr,greater<int>())+1},
nc[4]={*lower_bound(all(rs[tr]),tc)-1,*lower_bound(all(rrs[tr]),tc,greater<int>())+1,tc,tc},
np,
cost[4];
if(flag) for(int t=0;t<4;t++) cost[t]=1;
else{
for(int t=0;t<4;t++){
cost[t]=inf;
for(int u=0;u<4;u++)
cost[t]=min(cost[t],abs(tr-nr[u])+abs(tc-nc[u])+1);
}
}
for(int t=0;t<4;t++){
np=nr[t]*C+nc[t];
if(D[np]>u.vc+cost[t]) q.push(pii(D[np]=u.vc+cost[t],np));
}
}
}
int main(){
input();
dijk();
printf("%d\n",D[e]);
}
Compilation message
portals.cpp: In function 'void input()':
portals.cpp:14:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d\n",&R,&C);
^
portals.cpp:15:47: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=1;i<=R;i++) scanf("%s",buf[i]+1);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
7056 KB |
Output is correct |
2 |
Correct |
0 ms |
7056 KB |
Output is correct |
3 |
Correct |
0 ms |
7056 KB |
Output is correct |
4 |
Correct |
0 ms |
7056 KB |
Output is correct |
5 |
Correct |
0 ms |
7056 KB |
Output is correct |
6 |
Correct |
0 ms |
7056 KB |
Output is correct |
7 |
Correct |
0 ms |
7056 KB |
Output is correct |
8 |
Correct |
0 ms |
7056 KB |
Output is correct |
9 |
Correct |
0 ms |
7056 KB |
Output is correct |
10 |
Correct |
0 ms |
7056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
7056 KB |
Output is correct |
2 |
Correct |
0 ms |
7056 KB |
Output is correct |
3 |
Correct |
0 ms |
7056 KB |
Output is correct |
4 |
Correct |
0 ms |
7056 KB |
Output is correct |
5 |
Correct |
0 ms |
7056 KB |
Output is correct |
6 |
Correct |
0 ms |
7056 KB |
Output is correct |
7 |
Correct |
0 ms |
7056 KB |
Output is correct |
8 |
Correct |
0 ms |
7056 KB |
Output is correct |
9 |
Correct |
0 ms |
7056 KB |
Output is correct |
10 |
Correct |
0 ms |
7056 KB |
Output is correct |
11 |
Correct |
0 ms |
7056 KB |
Output is correct |
12 |
Correct |
0 ms |
7056 KB |
Output is correct |
13 |
Correct |
0 ms |
7056 KB |
Output is correct |
14 |
Correct |
0 ms |
7056 KB |
Output is correct |
15 |
Correct |
0 ms |
7056 KB |
Output is correct |
16 |
Correct |
0 ms |
7056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
7056 KB |
Output is correct |
2 |
Correct |
0 ms |
7056 KB |
Output is correct |
3 |
Correct |
0 ms |
7056 KB |
Output is correct |
4 |
Correct |
0 ms |
7056 KB |
Output is correct |
5 |
Correct |
6 ms |
7452 KB |
Output is correct |
6 |
Correct |
6 ms |
7452 KB |
Output is correct |
7 |
Correct |
9 ms |
7452 KB |
Output is correct |
8 |
Correct |
3 ms |
7452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
7056 KB |
Output is correct |
2 |
Correct |
0 ms |
7056 KB |
Output is correct |
3 |
Correct |
0 ms |
7056 KB |
Output is correct |
4 |
Correct |
0 ms |
7056 KB |
Output is correct |
5 |
Correct |
0 ms |
7056 KB |
Output is correct |
6 |
Correct |
0 ms |
7056 KB |
Output is correct |
7 |
Correct |
0 ms |
7056 KB |
Output is correct |
8 |
Correct |
0 ms |
7056 KB |
Output is correct |
9 |
Correct |
0 ms |
7056 KB |
Output is correct |
10 |
Correct |
0 ms |
7056 KB |
Output is correct |
11 |
Correct |
0 ms |
7056 KB |
Output is correct |
12 |
Correct |
0 ms |
7056 KB |
Output is correct |
13 |
Correct |
0 ms |
7056 KB |
Output is correct |
14 |
Correct |
6 ms |
7452 KB |
Output is correct |
15 |
Correct |
9 ms |
7452 KB |
Output is correct |
16 |
Correct |
6 ms |
7452 KB |
Output is correct |
17 |
Correct |
6 ms |
7452 KB |
Output is correct |
18 |
Correct |
9 ms |
7320 KB |
Output is correct |
19 |
Correct |
9 ms |
7196 KB |
Output is correct |
20 |
Correct |
6 ms |
7200 KB |
Output is correct |
21 |
Correct |
6 ms |
7452 KB |
Output is correct |
22 |
Correct |
9 ms |
7452 KB |
Output is correct |
23 |
Correct |
9 ms |
7452 KB |
Output is correct |
24 |
Correct |
9 ms |
7056 KB |
Output is correct |
25 |
Correct |
0 ms |
7056 KB |
Output is correct |
26 |
Correct |
0 ms |
7056 KB |
Output is correct |
27 |
Correct |
0 ms |
7056 KB |
Output is correct |
28 |
Correct |
3 ms |
7452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
7056 KB |
Output is correct |
2 |
Correct |
0 ms |
7056 KB |
Output is correct |
3 |
Correct |
0 ms |
7056 KB |
Output is correct |
4 |
Correct |
0 ms |
7056 KB |
Output is correct |
5 |
Correct |
0 ms |
7056 KB |
Output is correct |
6 |
Correct |
0 ms |
7056 KB |
Output is correct |
7 |
Correct |
0 ms |
7056 KB |
Output is correct |
8 |
Correct |
0 ms |
7056 KB |
Output is correct |
9 |
Correct |
0 ms |
7056 KB |
Output is correct |
10 |
Correct |
0 ms |
7056 KB |
Output is correct |
11 |
Correct |
0 ms |
7056 KB |
Output is correct |
12 |
Correct |
0 ms |
7056 KB |
Output is correct |
13 |
Correct |
0 ms |
7056 KB |
Output is correct |
14 |
Correct |
6 ms |
7452 KB |
Output is correct |
15 |
Correct |
6 ms |
7452 KB |
Output is correct |
16 |
Correct |
13 ms |
7452 KB |
Output is correct |
17 |
Correct |
9 ms |
7452 KB |
Output is correct |
18 |
Correct |
13 ms |
7320 KB |
Output is correct |
19 |
Correct |
9 ms |
7196 KB |
Output is correct |
20 |
Correct |
6 ms |
7200 KB |
Output is correct |
21 |
Correct |
9 ms |
7452 KB |
Output is correct |
22 |
Correct |
6 ms |
7452 KB |
Output is correct |
23 |
Correct |
9 ms |
7452 KB |
Output is correct |
24 |
Correct |
313 ms |
13132 KB |
Output is correct |
25 |
Correct |
306 ms |
7452 KB |
Output is correct |
26 |
Correct |
259 ms |
7320 KB |
Output is correct |
27 |
Correct |
253 ms |
7328 KB |
Output is correct |
28 |
Correct |
216 ms |
14712 KB |
Output is correct |
29 |
Correct |
273 ms |
14580 KB |
Output is correct |
30 |
Correct |
333 ms |
14580 KB |
Output is correct |
31 |
Correct |
6 ms |
7056 KB |
Output is correct |
32 |
Correct |
253 ms |
7332 KB |
Output is correct |
33 |
Correct |
0 ms |
7056 KB |
Output is correct |
34 |
Correct |
0 ms |
7056 KB |
Output is correct |
35 |
Correct |
229 ms |
11280 KB |
Output is correct |
36 |
Correct |
0 ms |
7056 KB |
Output is correct |
37 |
Correct |
6 ms |
7452 KB |
Output is correct |
38 |
Correct |
176 ms |
13788 KB |
Output is correct |
39 |
Correct |
163 ms |
15108 KB |
Output is correct |