#include<cstdio>
#include<queue>
#include<memory.h>
#define origin 1061109567
#define MAX_N 1100
using namespace std;
struct data {
int x,y,v;
data() {
}
data(int x_,int y_,int v_) {
x = x_, y = y_, v = v_;
}
};
bool operator <(data X,data Y) {
return X.v > Y.v;
}
bool operator >(data X,data Y) {
return X.v < Y.v;
}
int n,m,ct,dir[5][2] = {{-1,0},{1,0},{0,1},{0,-1}};
char a[MAX_N][MAX_N];
int dist[MAX_N][MAX_N][4], sd[MAX_N][MAX_N], dp[MAX_N][MAX_N];
int x1,x2,y1,y2;
priority_queue<data> Q_;
inline void Push(int x,int y,int z) {
if(x>=0 && x<n && y>=0 && y<m && a[x][y]!='#' && dp[x][y]==origin) {
Q_.push(data(x,y,z));
}
}
int main()
{
memset(sd,-1,sizeof(sd));
memset(dp,0x3f,sizeof(dp));
scanf("%d %d",&n,&m);
for(int i=0;i<n;i++) scanf("%s",a[i]);
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
if(a[i][j] == 'S') x1=i, y1=j;
if(a[i][j] == 'C') x2=i, y2=j;
}
}
for(int i=0;i<n;i++) dist[i][0][0] = (a[i][0]!='#' ? 1:0);
for(int i=0;i<n;i++) dist[i][m-1][1] = (a[i][m-1]!='#' ? 1:0);
for(int i=0;i<m;i++) dist[0][i][2] = (a[0][i]!='#' ? 1:0);
for(int i=0;i<m;i++) dist[n-1][i][3] = (a[n-1][i]!='#' ? 1:0);
for(int i=0;i<n;i++)
for(int j=1;j<m;j++) dist[i][j][0] = (a[i][j-1]!='#' ? dist[i][j-1][0]+1:1);
for(int i=0;i<n;i++)
for(int j=m-2;j>=0;j--) dist[i][j][1] = (a[i][j+1]!='#' ? dist[i][j+1][1]+1:1);
for(int i=0;i<m;i++)
for(int j=1;j<n;j++) dist[j][i][2] = (a[j-1][i]!='#' ? dist[j-1][i][2]+1:1);
for(int i=0;i<m;i++)
for(int j=n-1;j>=0;j--) dist[j][i][3] = (a[j+1][i]!='#' ? dist[j+1][i][3]+1:1);
Q_.push(data(x1,y1,1));
while(dp[x2][y2] == origin) {
data T = Q_.top();
Q_.pop();
if(dp[T.x][T.y]==origin) {
dp[T.x][T.y] = T.v;
for(int d=0;d<4;d++) Push(T.x+dir[d][0],T.y+dir[d][1],T.v+1);
int shortest = min(dist[T.x][T.y][2],dist[T.x][T.y][3]);
shortest = min(shortest,min(dist[T.x][T.y][0],dist[T.x][T.y][1]));
Push(T.x,T.y-dist[T.x][T.y][0]+1,T.v+shortest);
Push(T.x,T.y+dist[T.x][T.y][1]-1,T.v+shortest);
Push(T.x-dist[T.x][T.y][2]+1,T.y,T.v+shortest);
Push(T.x+dist[T.x][T.y][3]-1,T.y,T.v+shortest);
}
}
printf("%d\n\n",dp[x2][y2]-1);
/*
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) printf("%d ",dp[i][j]);
printf("\n");
}
printf("\n");
for(int d=0;d<3;d++) {
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) printf("%d ",dist[i][j][d]);
printf("\n");
}
printf("\n");
}
*/
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
30756 KB |
Output is correct |
2 |
Correct |
2 ms |
30756 KB |
Output is correct |
3 |
Correct |
0 ms |
30756 KB |
Output is correct |
4 |
Correct |
0 ms |
30756 KB |
Output is correct |
5 |
Correct |
0 ms |
30756 KB |
Output is correct |
6 |
Correct |
0 ms |
30756 KB |
Output is correct |
7 |
Correct |
0 ms |
30756 KB |
Output is correct |
8 |
Correct |
0 ms |
30756 KB |
Output is correct |
9 |
Correct |
0 ms |
30756 KB |
Output is correct |
10 |
Correct |
0 ms |
30756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
30756 KB |
Output is correct |
2 |
Correct |
0 ms |
30756 KB |
Output is correct |
3 |
Correct |
3 ms |
30756 KB |
Output is correct |
4 |
Correct |
0 ms |
30756 KB |
Output is correct |
5 |
Correct |
0 ms |
30756 KB |
Output is correct |
6 |
Correct |
3 ms |
30756 KB |
Output is correct |
7 |
Correct |
0 ms |
30756 KB |
Output is correct |
8 |
Correct |
0 ms |
30756 KB |
Output is correct |
9 |
Correct |
4 ms |
30756 KB |
Output is correct |
10 |
Correct |
0 ms |
30948 KB |
Output is correct |
11 |
Correct |
0 ms |
30756 KB |
Output is correct |
12 |
Correct |
0 ms |
30756 KB |
Output is correct |
13 |
Correct |
0 ms |
30756 KB |
Output is correct |
14 |
Correct |
0 ms |
30756 KB |
Output is correct |
15 |
Correct |
3 ms |
30756 KB |
Output is correct |
16 |
Correct |
0 ms |
30756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
30756 KB |
Output is correct |
2 |
Correct |
0 ms |
30756 KB |
Output is correct |
3 |
Correct |
0 ms |
30756 KB |
Output is correct |
4 |
Correct |
0 ms |
30756 KB |
Output is correct |
5 |
Correct |
9 ms |
30756 KB |
Output is correct |
6 |
Correct |
7 ms |
30756 KB |
Output is correct |
7 |
Correct |
6 ms |
30756 KB |
Output is correct |
8 |
Correct |
7 ms |
30756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
30756 KB |
Output is correct |
2 |
Correct |
0 ms |
30756 KB |
Output is correct |
3 |
Correct |
0 ms |
30756 KB |
Output is correct |
4 |
Correct |
0 ms |
30756 KB |
Output is correct |
5 |
Correct |
0 ms |
30756 KB |
Output is correct |
6 |
Correct |
0 ms |
30756 KB |
Output is correct |
7 |
Correct |
0 ms |
30756 KB |
Output is correct |
8 |
Correct |
0 ms |
30756 KB |
Output is correct |
9 |
Correct |
0 ms |
30756 KB |
Output is correct |
10 |
Correct |
4 ms |
30948 KB |
Output is correct |
11 |
Correct |
0 ms |
30756 KB |
Output is correct |
12 |
Correct |
0 ms |
30756 KB |
Output is correct |
13 |
Correct |
0 ms |
30756 KB |
Output is correct |
14 |
Correct |
4 ms |
30756 KB |
Output is correct |
15 |
Correct |
7 ms |
30756 KB |
Output is correct |
16 |
Correct |
9 ms |
30756 KB |
Output is correct |
17 |
Correct |
7 ms |
30756 KB |
Output is correct |
18 |
Correct |
13 ms |
30756 KB |
Output is correct |
19 |
Correct |
0 ms |
30948 KB |
Output is correct |
20 |
Correct |
8 ms |
33064 KB |
Output is correct |
21 |
Correct |
4 ms |
30756 KB |
Output is correct |
22 |
Correct |
9 ms |
30756 KB |
Output is correct |
23 |
Correct |
10 ms |
30756 KB |
Output is correct |
24 |
Correct |
10 ms |
30756 KB |
Output is correct |
25 |
Correct |
0 ms |
30756 KB |
Output is correct |
26 |
Correct |
3 ms |
30756 KB |
Output is correct |
27 |
Correct |
0 ms |
30756 KB |
Output is correct |
28 |
Correct |
3 ms |
30756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
30756 KB |
Output is correct |
2 |
Correct |
3 ms |
30756 KB |
Output is correct |
3 |
Correct |
0 ms |
30756 KB |
Output is correct |
4 |
Correct |
0 ms |
30756 KB |
Output is correct |
5 |
Correct |
0 ms |
30756 KB |
Output is correct |
6 |
Correct |
0 ms |
30756 KB |
Output is correct |
7 |
Correct |
0 ms |
30756 KB |
Output is correct |
8 |
Correct |
0 ms |
30756 KB |
Output is correct |
9 |
Correct |
0 ms |
30756 KB |
Output is correct |
10 |
Correct |
3 ms |
30948 KB |
Output is correct |
11 |
Correct |
0 ms |
30756 KB |
Output is correct |
12 |
Correct |
3 ms |
30756 KB |
Output is correct |
13 |
Correct |
0 ms |
30756 KB |
Output is correct |
14 |
Correct |
6 ms |
30756 KB |
Output is correct |
15 |
Correct |
10 ms |
30756 KB |
Output is correct |
16 |
Correct |
4 ms |
30756 KB |
Output is correct |
17 |
Correct |
7 ms |
30756 KB |
Output is correct |
18 |
Correct |
9 ms |
30756 KB |
Output is correct |
19 |
Correct |
0 ms |
30948 KB |
Output is correct |
20 |
Correct |
13 ms |
33064 KB |
Output is correct |
21 |
Correct |
6 ms |
30756 KB |
Output is correct |
22 |
Correct |
3 ms |
30756 KB |
Output is correct |
23 |
Correct |
3 ms |
30756 KB |
Output is correct |
24 |
Correct |
290 ms |
31140 KB |
Output is correct |
25 |
Correct |
368 ms |
31912 KB |
Output is correct |
26 |
Correct |
65 ms |
39976 KB |
Output is correct |
27 |
Correct |
316 ms |
67624 KB |
Output is correct |
28 |
Correct |
145 ms |
30756 KB |
Output is correct |
29 |
Correct |
169 ms |
30756 KB |
Output is correct |
30 |
Correct |
160 ms |
30756 KB |
Output is correct |
31 |
Correct |
11 ms |
30756 KB |
Output is correct |
32 |
Correct |
316 ms |
30948 KB |
Output is correct |
33 |
Correct |
0 ms |
30756 KB |
Output is correct |
34 |
Correct |
3 ms |
30756 KB |
Output is correct |
35 |
Correct |
137 ms |
30948 KB |
Output is correct |
36 |
Correct |
0 ms |
30756 KB |
Output is correct |
37 |
Correct |
7 ms |
30756 KB |
Output is correct |
38 |
Correct |
86 ms |
30756 KB |
Output is correct |
39 |
Correct |
80 ms |
30756 KB |
Output is correct |