#include <bits/stdc++.h>
#define va first
#define vb second
using namespace std;
const int MAX = 1005;
typedef pair<int,int> pii;
typedef pair<int,pii> ppi;
int R,C,sx,sy,ex,ey,cnt,dis[MAX][MAX],l[MAX][MAX],r[MAX][MAX],u[MAX][MAX],d[MAX][MAX];
bool init[MAX][MAX];
char c;
priority_queue<ppi> PQ;
int main()
{
cin >> R >> C;
for(int i=1; i<=R; i++){
for(int j=1; j<=C; j++){
cin >> c;
if(c=='.') init[i][j] = 1;
dis[i][j] = 1e6;
}
}
cin >> sy >> sx >> ey >> ex;
for(int i=1; i<=R; i++){
for(int j=1; j<=C; j++){
if(init[i][j]) l[i][j] = cnt;
else cnt = j;
}
for(int j=C; j>=1; j--){
if(init[i][j]) r[i][j] = cnt;
else cnt = j;
}
}
for(int j=1; j<=C; j++){
for(int i=1; i<=R; i++){
if(init[i][j]) u[i][j] = cnt;
else cnt = i;
}
for(int i=R; i>=1; i--){
if(init[i][j]) d[i][j] = cnt;
else cnt = i;
}
}
dis[sy][sx] = 0;
int D,y,x,val;
PQ.push(ppi(0,pii(sy,sx)));
while(!PQ.empty()){
D = -PQ.top().va;
y = PQ.top().vb.va;
x = PQ.top().vb.vb;
PQ.pop();
if(D>dis[y][x]) continue;
val = l[y][x]+1;
if(dis[y][val]>D+1){
dis[y][val] = D+1;
PQ.push(ppi(-D-1,pii(y,val)));
}
val = r[y][x]-1;
if(dis[y][val]>D+1){
dis[y][val] = D+1;
PQ.push(ppi(-D-1,pii(y,val)));
}
val = u[y][x]+1;
if(dis[val][x]>D+1){
dis[val][x] = D+1;
PQ.push(ppi(-D-1,pii(val,x)));
}
val = d[y][x]-1;
if(dis[val][x]>D+1){
dis[val][x] = D+1;
PQ.push(ppi(-D-1,pii(val,x)));
}
val = x+1;
if(init[y][val] && dis[y][val]>D+2){
dis[y][val] = D+2;
PQ.push(ppi(-D-2,pii(y,val)));
}
val = x-1;
if(init[y][val] && dis[y][val]>D+2){
dis[y][val] = D+2;
PQ.push(ppi(-D-2,pii(y,val)));
}
val = y+1;
if(init[val][x] && dis[val][x]>D+2){
dis[val][x] = D+2;
PQ.push(ppi(-D-2,pii(val,x)));
}
val = y-1;
if(init[val][x] && dis[val][x]>D+2){
dis[val][x] = D+2;
PQ.push(ppi(-D-2,pii(val,x)));
}
}
if(dis[ey][ex]==1e6) cout << -1;
else cout << dis[ey][ex];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
3 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
3 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
3 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
3 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
3 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
3 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
17 ms |
4480 KB |
Output is correct |
12 |
Correct |
16 ms |
4480 KB |
Output is correct |
13 |
Correct |
21 ms |
4608 KB |
Output is correct |
14 |
Correct |
18 ms |
4480 KB |
Output is correct |
15 |
Correct |
10 ms |
4480 KB |
Output is correct |
16 |
Correct |
17 ms |
4608 KB |
Output is correct |
17 |
Correct |
18 ms |
4480 KB |
Output is correct |
18 |
Correct |
12 ms |
4480 KB |
Output is correct |
19 |
Correct |
17 ms |
4480 KB |
Output is correct |
20 |
Correct |
13 ms |
4224 KB |
Output is correct |
21 |
Correct |
12 ms |
4480 KB |
Output is correct |
22 |
Correct |
3 ms |
4352 KB |
Output is correct |
23 |
Correct |
14 ms |
4480 KB |
Output is correct |
24 |
Correct |
16 ms |
4480 KB |
Output is correct |
25 |
Correct |
13 ms |
4480 KB |
Output is correct |
26 |
Correct |
17 ms |
4480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
3 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
3 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
3 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
17 ms |
4480 KB |
Output is correct |
12 |
Correct |
16 ms |
4480 KB |
Output is correct |
13 |
Correct |
21 ms |
4608 KB |
Output is correct |
14 |
Correct |
18 ms |
4480 KB |
Output is correct |
15 |
Correct |
10 ms |
4480 KB |
Output is correct |
16 |
Correct |
17 ms |
4608 KB |
Output is correct |
17 |
Correct |
18 ms |
4480 KB |
Output is correct |
18 |
Correct |
12 ms |
4480 KB |
Output is correct |
19 |
Correct |
17 ms |
4480 KB |
Output is correct |
20 |
Correct |
13 ms |
4224 KB |
Output is correct |
21 |
Correct |
12 ms |
4480 KB |
Output is correct |
22 |
Correct |
3 ms |
4352 KB |
Output is correct |
23 |
Correct |
14 ms |
4480 KB |
Output is correct |
24 |
Correct |
16 ms |
4480 KB |
Output is correct |
25 |
Correct |
13 ms |
4480 KB |
Output is correct |
26 |
Correct |
17 ms |
4480 KB |
Output is correct |
27 |
Correct |
545 ms |
21288 KB |
Output is correct |
28 |
Correct |
588 ms |
22896 KB |
Output is correct |
29 |
Correct |
632 ms |
23660 KB |
Output is correct |
30 |
Correct |
489 ms |
22748 KB |
Output is correct |
31 |
Correct |
126 ms |
22008 KB |
Output is correct |
32 |
Correct |
362 ms |
22240 KB |
Output is correct |
33 |
Correct |
66 ms |
11000 KB |
Output is correct |
34 |
Correct |
173 ms |
22008 KB |
Output is correct |
35 |
Correct |
365 ms |
22392 KB |
Output is correct |
36 |
Correct |
292 ms |
22136 KB |
Output is correct |
37 |
Correct |
154 ms |
22136 KB |
Output is correct |
38 |
Correct |
581 ms |
22888 KB |
Output is correct |
39 |
Correct |
157 ms |
22008 KB |
Output is correct |
40 |
Correct |
125 ms |
21968 KB |
Output is correct |