#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int xx[] = {1, 0, -1, 0};
const int yy[] = {0, 1, 0, -1};
struct dat{
int x, y, d, w;
dat(){}
dat(int x, int y, int d, int w): x(x), y(y), d(d), w(w){}
bool operator<(const dat &r)const{
return w>r.w;
}
};
int n, m;
bool arr[1005][1005];
int wallX[1005][1005][4], wallY[1005][1005][4];
bool visited[1005][1005][5];
int r1, c1;
int r2, c2;
priority_queue<dat> que;
void makeWall(){
for(int i=1; i<=n; i++){
for(int j=m; j>=1; j--){
if(arr[i][j]) wallX[i][j][1] = i, wallY[i][j][1] = j-1;
else wallX[i][j][1] = wallX[i][j+1][1], wallY[i][j][1] = wallY[i][j+1][1];
}
}
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
if(arr[i][j]) wallX[i][j][3] = i, wallY[i][j][3] = j+1;
else wallX[i][j][3] = wallX[i][j-1][3], wallY[i][j][3] = wallY[i][j-1][3];
}
}
for(int i=n; i>=1; i--){
for(int j=1; j<=m; j++){
if(arr[i][j]) wallX[i][j][0] = i-1, wallY[i][j][0] = j;
else wallX[i][j][0] = wallX[i+1][j][0], wallY[i][j][0] = wallY[i+1][j][0];
}
}
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
if(arr[i][j]) wallX[i][j][2] = i+1, wallY[i][j][2] = j;
else wallX[i][j][2] = wallX[i-1][j][2], wallY[i][j][2] = wallY[i-1][j][2];
}
}
}
void queueInsert(int x, int y, int d, int w){
if(visited[x][y][d] || arr[x][y]) return;
que.push(dat(x, y, d, w));
}
int main(){
scanf("%d %d", &n, &m);
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
char c;
scanf(" %c", &c);
arr[i][j] = (c == '#');
}
}
scanf("%d %d %d %d", &r1, &c1, &r2, &c2);
makeWall();
que.push(dat(r1, c1, 4, 0));
while(!que.empty()){
dat tmp = que.top(); que.pop();
int x = tmp.x, y = tmp.y, d = tmp.d, w = tmp.w;
if(visited[tmp.x][tmp.y][tmp.d]) continue;
visited[tmp.x][tmp.y][tmp.d] = 1;
// printf("%d %d %d %d\n", x, y, d, w);
if(x == r2 && y == c2){
printf("%d", w);
return 0;
}
for(int i=0; i<4; i++){
queueInsert(wallX[x][y][i], wallY[x][y][i], i, w+1);
queueInsert(x+xx[i], y+yy[i], i^2, w+2);
}
}
printf("-1");
}
Compilation message
skating.cpp: In function 'int main()':
skating.cpp:77:35: warning: unused variable 'd' [-Wunused-variable]
77 | int x = tmp.x, y = tmp.y, d = tmp.d, w = tmp.w;
| ^
skating.cpp:62:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
skating.cpp:66:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
66 | scanf(" %c", &c);
| ~~~~~^~~~~~~~~~~
skating.cpp:70:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
70 | scanf("%d %d %d %d", &r1, &c1, &r2, &c2);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
20 ms |
4684 KB |
Output is correct |
12 |
Correct |
24 ms |
4956 KB |
Output is correct |
13 |
Correct |
10 ms |
5444 KB |
Output is correct |
14 |
Correct |
22 ms |
4424 KB |
Output is correct |
15 |
Correct |
4 ms |
3404 KB |
Output is correct |
16 |
Correct |
40 ms |
5432 KB |
Output is correct |
17 |
Correct |
28 ms |
4420 KB |
Output is correct |
18 |
Correct |
21 ms |
4428 KB |
Output is correct |
19 |
Correct |
29 ms |
4616 KB |
Output is correct |
20 |
Correct |
11 ms |
3896 KB |
Output is correct |
21 |
Correct |
15 ms |
4300 KB |
Output is correct |
22 |
Correct |
8 ms |
4172 KB |
Output is correct |
23 |
Correct |
29 ms |
4544 KB |
Output is correct |
24 |
Correct |
31 ms |
4532 KB |
Output is correct |
25 |
Correct |
16 ms |
4428 KB |
Output is correct |
26 |
Correct |
27 ms |
4580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
20 ms |
4684 KB |
Output is correct |
12 |
Correct |
24 ms |
4956 KB |
Output is correct |
13 |
Correct |
10 ms |
5444 KB |
Output is correct |
14 |
Correct |
22 ms |
4424 KB |
Output is correct |
15 |
Correct |
4 ms |
3404 KB |
Output is correct |
16 |
Correct |
40 ms |
5432 KB |
Output is correct |
17 |
Correct |
28 ms |
4420 KB |
Output is correct |
18 |
Correct |
21 ms |
4428 KB |
Output is correct |
19 |
Correct |
29 ms |
4616 KB |
Output is correct |
20 |
Correct |
11 ms |
3896 KB |
Output is correct |
21 |
Correct |
15 ms |
4300 KB |
Output is correct |
22 |
Correct |
8 ms |
4172 KB |
Output is correct |
23 |
Correct |
29 ms |
4544 KB |
Output is correct |
24 |
Correct |
31 ms |
4532 KB |
Output is correct |
25 |
Correct |
16 ms |
4428 KB |
Output is correct |
26 |
Correct |
27 ms |
4580 KB |
Output is correct |
27 |
Correct |
556 ms |
40536 KB |
Output is correct |
28 |
Correct |
1154 ms |
47004 KB |
Output is correct |
29 |
Correct |
68 ms |
38320 KB |
Output is correct |
30 |
Correct |
448 ms |
45116 KB |
Output is correct |
31 |
Correct |
79 ms |
33708 KB |
Output is correct |
32 |
Correct |
836 ms |
38936 KB |
Output is correct |
33 |
Correct |
115 ms |
15556 KB |
Output is correct |
34 |
Correct |
329 ms |
37636 KB |
Output is correct |
35 |
Correct |
777 ms |
40756 KB |
Output is correct |
36 |
Correct |
667 ms |
39892 KB |
Output is correct |
37 |
Correct |
250 ms |
37888 KB |
Output is correct |
38 |
Correct |
1337 ms |
46884 KB |
Output is correct |
39 |
Correct |
360 ms |
38536 KB |
Output is correct |
40 |
Correct |
173 ms |
38344 KB |
Output is correct |