#include <bits/stdc++.h>
using namespace std;
int yy[]={-1, 0, 1, 0}, xx[]={0, 1, 0, -1};
int N, M;
int sy, sx, ey, ex;
int D[1001][1001], E[1001][1001], F[1002][1002][4];
char A[1001][1002];
struct Z{
int d, y, x;
bool operator < (const Z &ot)const{
return d > ot.d;
}
};
inline bool can_move(int y, int x){ return !(y < 1 || y > N || x < 1 || x > M || A[y][x] == '#'); }
void portal_bfs()
{
queue <int> que;
for (int i=1;i<=N;i++) for (int j=1;j<=M;j++) if (can_move(i, j)){
bool sw = 0;
for (int d=0;d<4;d++){
int ny = i+yy[d], nx = j+xx[d];
if (!can_move(ny, nx)){ sw = 1; break; }
}
if (!sw) continue;
E[i][j] = 0; que.push(i), que.push(j);
}
while (!que.empty()){
int y = que.front(); que.pop();
int x = que.front(); que.pop();
for (int d=0;d<4;d++){
int ny = y+yy[d], nx = x+xx[d];
if (!can_move(ny, nx)) continue;
if (E[ny][nx] == 2e9)
E[ny][nx] = E[y][x]+1, que.push(ny), que.push(nx);
}
}
}
int main()
{
scanf("%d%d", &N, &M);
for (int i=1;i<=N;i++) scanf("%s", A[i]+1);
for (int i=1;i<=N;i++) for (int j=1;j<=M;j++){
D[i][j] = E[i][j] = 2e9;
if (A[i][j] == 'S') sy = i, sx = j;
if (A[i][j] == 'C') ey = i, ex = j;
}
for (int i=1;i<=N;i++) for (int j=1;j<=M;j++){
F[i][j][0] = A[i][j] == '#' ? 0 : F[i-1][j][0]+1;
F[i][j][3] = A[i][j] == '#' ? 0 : F[i][j-1][3]+1;
}
for (int i=N;i;i--) for (int j=M;j;j--){
F[i][j][1] = A[i][j] == '#' ? 0 : F[i][j+1][1]+1;
F[i][j][2] = A[i][j] == '#' ? 0 : F[i+1][j][2]+1;
}
portal_bfs();
priority_queue <Z> que;
D[sy][sx] = 0; que.push({0, sy, sx});
while (!que.empty()){
Z q = que.top(); que.pop();
if (q.d != D[q.y][q.x]) continue;
for (int d=0;d<4;d++){
int y = q.y+yy[d], x = q.x+xx[d];
if (!can_move(y, x)) continue;
if (D[y][x] > q.d+1)
D[y][x] = q.d+1, que.push({D[y][x], y, x});
y = q.y + yy[d] * (F[q.y][q.x][d]-1);
x = q.x + xx[d] * (F[q.y][q.x][d]-1);
if (D[y][x] > q.d+E[q.y][q.x]+1)
D[y][x] = q.d+E[q.y][q.x]+1, que.push({D[y][x], y, x});
}
}
printf("%d\n", D[ey][ex]);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
26220 KB |
Output is correct |
2 |
Correct |
0 ms |
26220 KB |
Output is correct |
3 |
Correct |
0 ms |
26220 KB |
Output is correct |
4 |
Correct |
0 ms |
26220 KB |
Output is correct |
5 |
Correct |
0 ms |
26220 KB |
Output is correct |
6 |
Correct |
0 ms |
26220 KB |
Output is correct |
7 |
Correct |
0 ms |
26220 KB |
Output is correct |
8 |
Correct |
0 ms |
26220 KB |
Output is correct |
9 |
Correct |
0 ms |
26220 KB |
Output is correct |
10 |
Correct |
0 ms |
26220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
26220 KB |
Output is correct |
2 |
Correct |
0 ms |
26220 KB |
Output is correct |
3 |
Correct |
0 ms |
26220 KB |
Output is correct |
4 |
Correct |
0 ms |
26220 KB |
Output is correct |
5 |
Correct |
0 ms |
26220 KB |
Output is correct |
6 |
Correct |
0 ms |
26220 KB |
Output is correct |
7 |
Correct |
0 ms |
26220 KB |
Output is correct |
8 |
Correct |
0 ms |
26220 KB |
Output is correct |
9 |
Correct |
0 ms |
26220 KB |
Output is correct |
10 |
Correct |
0 ms |
26220 KB |
Output is correct |
11 |
Correct |
0 ms |
26220 KB |
Output is correct |
12 |
Correct |
0 ms |
26220 KB |
Output is correct |
13 |
Correct |
0 ms |
26220 KB |
Output is correct |
14 |
Correct |
0 ms |
26220 KB |
Output is correct |
15 |
Correct |
0 ms |
26220 KB |
Output is correct |
16 |
Correct |
0 ms |
26220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
26220 KB |
Output is correct |
2 |
Correct |
0 ms |
26220 KB |
Output is correct |
3 |
Correct |
0 ms |
26220 KB |
Output is correct |
4 |
Correct |
0 ms |
26220 KB |
Output is correct |
5 |
Correct |
5 ms |
26348 KB |
Output is correct |
6 |
Correct |
4 ms |
26348 KB |
Output is correct |
7 |
Correct |
9 ms |
26348 KB |
Output is correct |
8 |
Correct |
3 ms |
26348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
26220 KB |
Output is correct |
2 |
Correct |
0 ms |
26220 KB |
Output is correct |
3 |
Correct |
0 ms |
26220 KB |
Output is correct |
4 |
Correct |
0 ms |
26220 KB |
Output is correct |
5 |
Correct |
0 ms |
26220 KB |
Output is correct |
6 |
Correct |
0 ms |
26220 KB |
Output is correct |
7 |
Correct |
0 ms |
26220 KB |
Output is correct |
8 |
Correct |
0 ms |
26220 KB |
Output is correct |
9 |
Correct |
0 ms |
26220 KB |
Output is correct |
10 |
Correct |
0 ms |
26220 KB |
Output is correct |
11 |
Correct |
0 ms |
26220 KB |
Output is correct |
12 |
Correct |
0 ms |
26220 KB |
Output is correct |
13 |
Correct |
0 ms |
26220 KB |
Output is correct |
14 |
Correct |
3 ms |
26348 KB |
Output is correct |
15 |
Correct |
0 ms |
26348 KB |
Output is correct |
16 |
Correct |
6 ms |
26348 KB |
Output is correct |
17 |
Correct |
8 ms |
26348 KB |
Output is correct |
18 |
Correct |
10 ms |
26348 KB |
Output is correct |
19 |
Correct |
10 ms |
26220 KB |
Output is correct |
20 |
Correct |
7 ms |
26220 KB |
Output is correct |
21 |
Correct |
4 ms |
26348 KB |
Output is correct |
22 |
Correct |
7 ms |
26348 KB |
Output is correct |
23 |
Correct |
5 ms |
26348 KB |
Output is correct |
24 |
Correct |
10 ms |
26220 KB |
Output is correct |
25 |
Correct |
0 ms |
26220 KB |
Output is correct |
26 |
Correct |
0 ms |
26220 KB |
Output is correct |
27 |
Correct |
0 ms |
26220 KB |
Output is correct |
28 |
Correct |
3 ms |
26348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
26220 KB |
Output is correct |
2 |
Correct |
0 ms |
26220 KB |
Output is correct |
3 |
Correct |
0 ms |
26220 KB |
Output is correct |
4 |
Correct |
0 ms |
26220 KB |
Output is correct |
5 |
Correct |
0 ms |
26220 KB |
Output is correct |
6 |
Correct |
0 ms |
26220 KB |
Output is correct |
7 |
Correct |
0 ms |
26220 KB |
Output is correct |
8 |
Correct |
0 ms |
26220 KB |
Output is correct |
9 |
Correct |
0 ms |
26220 KB |
Output is correct |
10 |
Correct |
0 ms |
26220 KB |
Output is correct |
11 |
Correct |
0 ms |
26220 KB |
Output is correct |
12 |
Correct |
0 ms |
26220 KB |
Output is correct |
13 |
Correct |
0 ms |
26220 KB |
Output is correct |
14 |
Correct |
5 ms |
26348 KB |
Output is correct |
15 |
Correct |
4 ms |
26348 KB |
Output is correct |
16 |
Correct |
0 ms |
26348 KB |
Output is correct |
17 |
Correct |
4 ms |
26348 KB |
Output is correct |
18 |
Correct |
6 ms |
26348 KB |
Output is correct |
19 |
Correct |
7 ms |
26220 KB |
Output is correct |
20 |
Correct |
7 ms |
26220 KB |
Output is correct |
21 |
Correct |
0 ms |
26348 KB |
Output is correct |
22 |
Correct |
3 ms |
26348 KB |
Output is correct |
23 |
Correct |
0 ms |
26348 KB |
Output is correct |
24 |
Correct |
166 ms |
30244 KB |
Output is correct |
25 |
Correct |
243 ms |
26604 KB |
Output is correct |
26 |
Correct |
245 ms |
26412 KB |
Output is correct |
27 |
Correct |
236 ms |
26412 KB |
Output is correct |
28 |
Correct |
133 ms |
30904 KB |
Output is correct |
29 |
Correct |
145 ms |
31036 KB |
Output is correct |
30 |
Correct |
169 ms |
31036 KB |
Output is correct |
31 |
Correct |
6 ms |
26220 KB |
Output is correct |
32 |
Correct |
216 ms |
26220 KB |
Output is correct |
33 |
Correct |
0 ms |
26220 KB |
Output is correct |
34 |
Correct |
0 ms |
26220 KB |
Output is correct |
35 |
Correct |
164 ms |
28232 KB |
Output is correct |
36 |
Correct |
0 ms |
26220 KB |
Output is correct |
37 |
Correct |
4 ms |
26348 KB |
Output is correct |
38 |
Correct |
90 ms |
31696 KB |
Output is correct |
39 |
Correct |
106 ms |
30376 KB |
Output is correct |