#include <stdio.h>
#include <queue>
#include <algorithm>
using namespace std;
struct mindol{int x,y,t;bool operator<(const mindol &a)const{return t>a.t;}};
priority_queue<mindol> pq;
char m[1010][1010];
int dt[4][1010][1010],dist[1010][1010];
int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
int w,h,gx,gy,flag;
int main(){
scanf("%d%d",&w,&h);
for(int i=1;i<=w;i++){
scanf("%s",m[i]+1);
}
for(int i=0;i<=w+1;i++)for(int j=0;j<4;j++)m[i][0]=m[i][h+1]='#',dt[j][i][0]=dt[j][i][h+1]=-1;
for(int i=0;i<=h+1;i++)for(int j=0;j<4;j++)m[0][i]=m[w+1][i]='#',dt[j][0][i]=dt[j][w+1][i]=-1;
for(int i=1;i<=w;i++){
for(int j=1;j<=h;j++){
dist[i][j]=987654321;
if(m[i][j]=='S')pq.push({i,j,0});
if(m[i][j]=='C')gx=i,gy=j;
dt[0][w+1-i][j]=( m[w+1-i][j]=='#' ? -1 :dt[0][w+2-i][j]+1);
dt[1][i][j]=(m[i][j]=='#' ? -1 : dt[1][i-1][j]+1);
dt[2][i][h+1-j]=(m[i][h+1-j]=='#' ? -1 : dt[2][i][h+2-j]+1);
dt[3][i][j]=(m[i][j]=='#' ? -1 : dt[3][i][j-1]+1);
}
}
mindol cur;
while(!pq.empty()){
cur=pq.top();
pq.pop();
if(m[cur.x][cur.y]=='#')continue;
if(dist[cur.x][cur.y]<=cur.t)continue;
dist[cur.x][cur.y]=cur.t;
for(int i=0;i<4;i++){
pq.push({cur.x+dx[i],cur.y+dy[i],cur.t+1});
}
for(int j=0;j<4;j++){
if(dt[j][cur.x][cur.y]<=1)continue;
if(cur.t+min(min(j==0?987654321:dt[0][cur.x][cur.y],j==1?987654321:dt[1][cur.x][cur.y]),
min(j==2?987654321:dt[2][cur.x][cur.y],j==3?987654321:dt[3][cur.x][cur.y]))+1>=
dist[cur.x+dx[j]*dt[j][cur.x][cur.y]][cur.y+dy[j]*dt[j][cur.x][cur.y]])continue;
pq.push({cur.x+dx[j]*dt[j][cur.x][cur.y],cur.y+dy[j]*dt[j][cur.x][cur.y],cur.t+min(min(j==0?987654321:dt[0][cur.x][cur.y],
j==1?987654321:dt[1][cur.x][cur.y]),
min(j==2?987654321:dt[2][cur.x][cur.y],
j==3?987654321:dt[3][cur.x][cur.y]))+1});
}
}
printf("%d",dist[gx][gy]);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
22132 KB |
Output is correct |
2 |
Correct |
0 ms |
22132 KB |
Output is correct |
3 |
Correct |
0 ms |
22132 KB |
Output is correct |
4 |
Correct |
0 ms |
22132 KB |
Output is correct |
5 |
Correct |
0 ms |
22132 KB |
Output is correct |
6 |
Correct |
0 ms |
22132 KB |
Output is correct |
7 |
Correct |
0 ms |
22132 KB |
Output is correct |
8 |
Correct |
0 ms |
22132 KB |
Output is correct |
9 |
Correct |
0 ms |
22132 KB |
Output is correct |
10 |
Correct |
0 ms |
22132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
22132 KB |
Output is correct |
2 |
Correct |
0 ms |
22132 KB |
Output is correct |
3 |
Correct |
0 ms |
22132 KB |
Output is correct |
4 |
Correct |
0 ms |
22132 KB |
Output is correct |
5 |
Correct |
0 ms |
22132 KB |
Output is correct |
6 |
Correct |
0 ms |
22132 KB |
Output is correct |
7 |
Correct |
0 ms |
22132 KB |
Output is correct |
8 |
Correct |
0 ms |
22132 KB |
Output is correct |
9 |
Correct |
0 ms |
22132 KB |
Output is correct |
10 |
Correct |
0 ms |
22324 KB |
Output is correct |
11 |
Correct |
2 ms |
22132 KB |
Output is correct |
12 |
Correct |
0 ms |
22132 KB |
Output is correct |
13 |
Correct |
2 ms |
22132 KB |
Output is correct |
14 |
Correct |
0 ms |
22132 KB |
Output is correct |
15 |
Correct |
0 ms |
22132 KB |
Output is correct |
16 |
Correct |
0 ms |
22132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
22132 KB |
Output is correct |
2 |
Correct |
0 ms |
22132 KB |
Output is correct |
3 |
Correct |
0 ms |
22132 KB |
Output is correct |
4 |
Correct |
0 ms |
22132 KB |
Output is correct |
5 |
Correct |
7 ms |
22132 KB |
Output is correct |
6 |
Correct |
13 ms |
22132 KB |
Output is correct |
7 |
Correct |
11 ms |
22132 KB |
Output is correct |
8 |
Correct |
6 ms |
22132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
22132 KB |
Output is correct |
2 |
Correct |
0 ms |
22132 KB |
Output is correct |
3 |
Correct |
0 ms |
22132 KB |
Output is correct |
4 |
Correct |
0 ms |
22132 KB |
Output is correct |
5 |
Correct |
0 ms |
22132 KB |
Output is correct |
6 |
Correct |
0 ms |
22132 KB |
Output is correct |
7 |
Correct |
0 ms |
22132 KB |
Output is correct |
8 |
Correct |
0 ms |
22132 KB |
Output is correct |
9 |
Correct |
0 ms |
22132 KB |
Output is correct |
10 |
Correct |
0 ms |
22324 KB |
Output is correct |
11 |
Correct |
0 ms |
22132 KB |
Output is correct |
12 |
Correct |
2 ms |
22132 KB |
Output is correct |
13 |
Correct |
0 ms |
22132 KB |
Output is correct |
14 |
Correct |
10 ms |
22132 KB |
Output is correct |
15 |
Correct |
14 ms |
22132 KB |
Output is correct |
16 |
Correct |
15 ms |
22132 KB |
Output is correct |
17 |
Correct |
11 ms |
22132 KB |
Output is correct |
18 |
Correct |
17 ms |
22324 KB |
Output is correct |
19 |
Correct |
24 ms |
22516 KB |
Output is correct |
20 |
Correct |
43 ms |
24440 KB |
Output is correct |
21 |
Correct |
6 ms |
22132 KB |
Output is correct |
22 |
Correct |
12 ms |
22132 KB |
Output is correct |
23 |
Correct |
10 ms |
22132 KB |
Output is correct |
24 |
Correct |
16 ms |
22132 KB |
Output is correct |
25 |
Correct |
0 ms |
22132 KB |
Output is correct |
26 |
Correct |
0 ms |
22132 KB |
Output is correct |
27 |
Correct |
0 ms |
22132 KB |
Output is correct |
28 |
Correct |
6 ms |
22132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
22132 KB |
Output is correct |
2 |
Correct |
0 ms |
22132 KB |
Output is correct |
3 |
Correct |
0 ms |
22132 KB |
Output is correct |
4 |
Correct |
0 ms |
22132 KB |
Output is correct |
5 |
Correct |
0 ms |
22132 KB |
Output is correct |
6 |
Correct |
0 ms |
22132 KB |
Output is correct |
7 |
Correct |
0 ms |
22132 KB |
Output is correct |
8 |
Correct |
0 ms |
22132 KB |
Output is correct |
9 |
Correct |
2 ms |
22132 KB |
Output is correct |
10 |
Correct |
3 ms |
22324 KB |
Output is correct |
11 |
Correct |
0 ms |
22132 KB |
Output is correct |
12 |
Correct |
0 ms |
22132 KB |
Output is correct |
13 |
Correct |
0 ms |
22132 KB |
Output is correct |
14 |
Correct |
10 ms |
22132 KB |
Output is correct |
15 |
Correct |
14 ms |
22132 KB |
Output is correct |
16 |
Correct |
11 ms |
22132 KB |
Output is correct |
17 |
Correct |
9 ms |
22132 KB |
Output is correct |
18 |
Correct |
17 ms |
22324 KB |
Output is correct |
19 |
Correct |
23 ms |
22516 KB |
Output is correct |
20 |
Correct |
43 ms |
24440 KB |
Output is correct |
21 |
Correct |
7 ms |
22132 KB |
Output is correct |
22 |
Correct |
8 ms |
22132 KB |
Output is correct |
23 |
Correct |
10 ms |
22132 KB |
Output is correct |
24 |
Correct |
449 ms |
22712 KB |
Output is correct |
25 |
Correct |
674 ms |
24440 KB |
Output is correct |
26 |
Execution timed out |
1000 ms |
40568 KB |
Program timed out |
27 |
Halted |
0 ms |
0 KB |
- |