#include"bits/stdc++.h"
using namespace std;
#define MP(a,b) make_pair(a,b)
int H,W;
string S[1000];
int beginY,beginX,endY,endX;
vector<int> l[1000],r[1000],u[1000],d[1000];
int cost[1000][1000];
int main(){
cin >> H >> W;
for(int i=0; i<H; i++) cin >> S[i];
cin >> beginY >> beginX >> endY >> endX;
beginY--; beginX--; endY--; endX--;
for(int i=0; i<H; i++){
for(int j=0; j<W; j++){
cost[i][j] = INT_MAX;
}
}
//
for(int i=1; i<H-1; i++){
for(int j=1; j<W-1; j++){
if(S[i][j] == '#') continue;
if(S[i-1][j] == '#') u[j].push_back(i);
if(S[i+1][j] == '#') d[j].push_back(i);
if(S[i][j-1] == '#') l[i].push_back(j);
if(S[i][j+1] == '#') r[i].push_back(j);
}
}
//
priority_queue< pair<int,pair<int,int> > > que;
que.push( MP(-0,MP(beginY, beginX)) );
cost[beginY][beginX] = 0;
while(!que.empty()){
int y = que.top().second.first;
int x = que.top().second.second;
int c = -que.top().first;
que.pop();
if(S[y-1][x] == '.' && cost[y-1][x] > c+2){
cost[y-1][x] = c+2;
que.push( MP(-c-2,MP(y-1, x)) );
}
if(S[y+1][x] == '.' && cost[y+1][x] > c+2){
cost[y+1][x] = c+2;
que.push( MP(-c-2,MP(y+1, x)) );
}
if(S[y][x-1] == '.' && cost[y][x-1] > c+2){
cost[y][x-1] = c+2;
que.push( MP(-c-2,MP(y, x-1)) );
}
if(S[y][x+1] == '.' && cost[y][x+1] > c+2){
cost[y][x+1] = c+2;
que.push( MP(-c-2,MP(y, x+1)) );
}
int edge = *(upper_bound(u[x].begin(), u[x].end(), y)-1);
if(cost[edge][x] > c+1){
cost[edge][x] = c+1;
que.push( MP(-c-1,MP(edge, x)) );
}
edge = *lower_bound(d[x].begin(), d[x].end(), y);
if(cost[edge][x] > c+1){
cost[edge][x] = c+1;
que.push( MP(-c-1,MP(edge, x)) );
}
edge = *(upper_bound(l[y].begin(), l[y].end(), x)-1);
if(cost[y][edge] > c+1){
cost[y][edge] = c+1;
que.push( MP(-c-1,MP(y, edge)) );
}
edge = *lower_bound(r[y].begin(), r[y].end(), x);
if(cost[y][edge] > c+1){
cost[y][edge] = c+1;
que.push( MP(-c-1,MP(y, edge)) );
}
}
//
/*for(int i=0; i<H; i++){
for(int j=0; j<W; j++){
cout << (cost[i][j] == INT_MAX ? -1 : cost[i][j]) << " ";
}
cout << endl;
}*/
if(cost[endY][endX] == INT_MAX) cout << -1 << endl;
else cout << cost[endY][endX] << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
512 KB |
Output is correct |
2 |
Correct |
2 ms |
556 KB |
Output is correct |
3 |
Correct |
3 ms |
572 KB |
Output is correct |
4 |
Correct |
3 ms |
484 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
484 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
512 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
512 KB |
Output is correct |
2 |
Correct |
2 ms |
556 KB |
Output is correct |
3 |
Correct |
3 ms |
572 KB |
Output is correct |
4 |
Correct |
3 ms |
484 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
484 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
512 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
12 ms |
1408 KB |
Output is correct |
12 |
Correct |
14 ms |
1408 KB |
Output is correct |
13 |
Correct |
18 ms |
1568 KB |
Output is correct |
14 |
Correct |
18 ms |
1664 KB |
Output is correct |
15 |
Correct |
6 ms |
1536 KB |
Output is correct |
16 |
Correct |
13 ms |
1536 KB |
Output is correct |
17 |
Correct |
12 ms |
1528 KB |
Output is correct |
18 |
Correct |
14 ms |
1600 KB |
Output is correct |
19 |
Correct |
13 ms |
1528 KB |
Output is correct |
20 |
Correct |
12 ms |
1280 KB |
Output is correct |
21 |
Correct |
14 ms |
1764 KB |
Output is correct |
22 |
Correct |
8 ms |
1408 KB |
Output is correct |
23 |
Correct |
13 ms |
1528 KB |
Output is correct |
24 |
Correct |
15 ms |
1464 KB |
Output is correct |
25 |
Correct |
16 ms |
1664 KB |
Output is correct |
26 |
Correct |
12 ms |
1536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
512 KB |
Output is correct |
2 |
Correct |
2 ms |
556 KB |
Output is correct |
3 |
Correct |
3 ms |
572 KB |
Output is correct |
4 |
Correct |
3 ms |
484 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
484 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
512 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
12 ms |
1408 KB |
Output is correct |
12 |
Correct |
14 ms |
1408 KB |
Output is correct |
13 |
Correct |
18 ms |
1568 KB |
Output is correct |
14 |
Correct |
18 ms |
1664 KB |
Output is correct |
15 |
Correct |
6 ms |
1536 KB |
Output is correct |
16 |
Correct |
13 ms |
1536 KB |
Output is correct |
17 |
Correct |
12 ms |
1528 KB |
Output is correct |
18 |
Correct |
14 ms |
1600 KB |
Output is correct |
19 |
Correct |
13 ms |
1528 KB |
Output is correct |
20 |
Correct |
12 ms |
1280 KB |
Output is correct |
21 |
Correct |
14 ms |
1764 KB |
Output is correct |
22 |
Correct |
8 ms |
1408 KB |
Output is correct |
23 |
Correct |
13 ms |
1528 KB |
Output is correct |
24 |
Correct |
15 ms |
1464 KB |
Output is correct |
25 |
Correct |
16 ms |
1664 KB |
Output is correct |
26 |
Correct |
12 ms |
1536 KB |
Output is correct |
27 |
Correct |
326 ms |
7720 KB |
Output is correct |
28 |
Correct |
441 ms |
8204 KB |
Output is correct |
29 |
Correct |
540 ms |
9204 KB |
Output is correct |
30 |
Correct |
493 ms |
10416 KB |
Output is correct |
31 |
Correct |
70 ms |
12540 KB |
Output is correct |
32 |
Correct |
207 ms |
7360 KB |
Output is correct |
33 |
Correct |
89 ms |
3960 KB |
Output is correct |
34 |
Correct |
166 ms |
9524 KB |
Output is correct |
35 |
Correct |
266 ms |
7984 KB |
Output is correct |
36 |
Correct |
301 ms |
9728 KB |
Output is correct |
37 |
Correct |
153 ms |
11616 KB |
Output is correct |
38 |
Correct |
555 ms |
8404 KB |
Output is correct |
39 |
Correct |
286 ms |
15440 KB |
Output is correct |
40 |
Correct |
103 ms |
9984 KB |
Output is correct |