#include <bits/stdc++.h>
#define va first
#define vb second
using namespace std;
const int MAX = 205;
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,v1,v2;
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;
v1 = l[y][x]+1;
v2 = x-1;
cnt = 1;
while(v1<=v2){
if(cnt%2){
if(dis[y][v1]>D+cnt){
dis[y][v1] = D+cnt;
PQ.push(ppi(-D-cnt,pii(y,v1)));
}
v1++;
}
else{
if(dis[y][v2]>D+cnt){
dis[y][v2] = D+cnt;
PQ.push(ppi(-D-cnt,pii(y,v2)));
}
v2--;
}
cnt++;
}
v1 = x+1;
v2 = r[y][x]-1;
cnt = 1;
while(v1<=v2){
if(cnt%2){
if(dis[y][v2]>D+cnt){
dis[y][v2] = D+cnt;
PQ.push(ppi(-D-cnt,pii(y,v2)));
}
v2--;
}
else{
if(dis[y][v1]>D+cnt){
dis[y][v1] = D+cnt;
PQ.push(ppi(-D-cnt,pii(y,v1)));
}
v1++;
}
cnt++;
}
v1 = u[y][x]+1;
v2 = y-1;
cnt = 1;
while(v1<=v2){
if(cnt%2){
if(dis[v1][x]>D+cnt){
dis[v1][x] = D+cnt;
PQ.push(ppi(-D-cnt,pii(v1,x)));
}
v1++;
}
else{
if(dis[v2][x]>D+cnt){
dis[v2][x] = D+cnt;
PQ.push(ppi(-D-cnt,pii(v2,x)));
}
v2--;
}
cnt++;
}
v1 = y+1;
v2 = d[y][x]-1;
cnt = 1;
while(v1<=v2){
if(cnt%2){
if(dis[v2][x]>D+cnt){
dis[v2][x] = D+cnt;
PQ.push(ppi(-D-cnt,pii(v2,x)));
}
v2--;
}
else{
if(dis[v1][x]>D+cnt){
dis[v1][x] = D+cnt;
PQ.push(ppi(-D-cnt,pii(v1,x)));
}
v1++;
}
cnt++;
}
}
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 |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
66 ms |
2172 KB |
Output is correct |
12 |
Correct |
75 ms |
2936 KB |
Output is correct |
13 |
Correct |
62 ms |
2936 KB |
Output is correct |
14 |
Correct |
21 ms |
1256 KB |
Output is correct |
15 |
Correct |
6 ms |
1152 KB |
Output is correct |
16 |
Correct |
36 ms |
2172 KB |
Output is correct |
17 |
Correct |
17 ms |
1252 KB |
Output is correct |
18 |
Correct |
12 ms |
1280 KB |
Output is correct |
19 |
Correct |
15 ms |
1408 KB |
Output is correct |
20 |
Correct |
63 ms |
1728 KB |
Output is correct |
21 |
Correct |
7 ms |
1280 KB |
Output is correct |
22 |
Correct |
13 ms |
1152 KB |
Output is correct |
23 |
Correct |
17 ms |
1272 KB |
Output is correct |
24 |
Correct |
28 ms |
1728 KB |
Output is correct |
25 |
Correct |
12 ms |
1280 KB |
Output is correct |
26 |
Correct |
13 ms |
1272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
66 ms |
2172 KB |
Output is correct |
12 |
Correct |
75 ms |
2936 KB |
Output is correct |
13 |
Correct |
62 ms |
2936 KB |
Output is correct |
14 |
Correct |
21 ms |
1256 KB |
Output is correct |
15 |
Correct |
6 ms |
1152 KB |
Output is correct |
16 |
Correct |
36 ms |
2172 KB |
Output is correct |
17 |
Correct |
17 ms |
1252 KB |
Output is correct |
18 |
Correct |
12 ms |
1280 KB |
Output is correct |
19 |
Correct |
15 ms |
1408 KB |
Output is correct |
20 |
Correct |
63 ms |
1728 KB |
Output is correct |
21 |
Correct |
7 ms |
1280 KB |
Output is correct |
22 |
Correct |
13 ms |
1152 KB |
Output is correct |
23 |
Correct |
17 ms |
1272 KB |
Output is correct |
24 |
Correct |
28 ms |
1728 KB |
Output is correct |
25 |
Correct |
12 ms |
1280 KB |
Output is correct |
26 |
Correct |
13 ms |
1272 KB |
Output is correct |
27 |
Runtime error |
16 ms |
1280 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
28 |
Halted |
0 ms |
0 KB |
- |