#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
const int inf = 1e9;
int n, m;
int sx, sy, ex, ey;
int A[1010][1010];
pii L[1010][1010], R[1010][1010], U[1010][1010], D[1010][1010];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int dist[1010][1010];
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);
if (c == '#') A[i][j] = 0;
else A[i][j] = 1;
}
}
scanf("%d %d", &sx, &sy);
scanf("%d %d", &ex, &ey);
for (int i=1; i<=n; i++) {
int p = 1;
for (int j=2; j<=m; j++) {
if (!A[i][j]) p = j;
else L[i][j] = {i, p+1};
}
p = m;
for (int j=m-1; j>=1; j--) {
if (!A[i][j]) p = j;
else R[i][j] = {i, p-1};
}
}
for (int j=1; j<=m; j++) {
int p = 1;
for (int i=2; i<=n; i++) {
if (!A[i][j]) p = i;
else U[i][j] = {p+1, j};
}
p = n;
for (int i=n-1; i>=1; i--) {
if (!A[i][j]) p = i;
else D[i][j] = {p-1, j};
}
}
for (int i=1; i<=n; i++) for (int j=1; j<=m; j++) dist[i][j] = inf;
priority_queue<tuple<int,int,int>> pq;
pq.push({0, sx, sy});
dist[sx][sy] = 0;
while (!pq.empty()) {
auto [c, x, y] = pq.top();
c = -c;
pq.pop();
if (dist[x][y] < c) continue;
for (int i=0; i<4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (!A[nx][ny]) continue;
if (dist[nx][ny] <= c + 2) continue;
dist[nx][ny] = c + 2;
pq.push({-c - 2, nx, ny});
}
for (auto [nx, ny] : {L[x][y], R[x][y], U[x][y], D[x][y]}) {
if (dist[nx][ny] <= c + 1) continue;
dist[nx][ny] = c + 1;
pq.push({-c - 1, nx, ny});
}
}
if (dist[ex][ey] >= inf) printf("-1\n");
else printf("%d\n", dist[ex][ey]);
}
Compilation message
skating.cpp: In function 'int main()':
skating.cpp:19:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
19 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
skating.cpp:23:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
23 | scanf(" %c", &c);
| ~~~~~^~~~~~~~~~~
skating.cpp:28:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
28 | scanf("%d %d", &sx, &sy);
| ~~~~~^~~~~~~~~~~~~~~~~~~
skating.cpp:29:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
29 | scanf("%d %d", &ex, &ey);
| ~~~~~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10584 KB |
Output is correct |
2 |
Correct |
1 ms |
10584 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10584 KB |
Output is correct |
6 |
Correct |
1 ms |
10584 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
10584 KB |
Output is correct |
10 |
Correct |
2 ms |
10584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10584 KB |
Output is correct |
2 |
Correct |
1 ms |
10584 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10584 KB |
Output is correct |
6 |
Correct |
1 ms |
10584 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
10584 KB |
Output is correct |
10 |
Correct |
2 ms |
10584 KB |
Output is correct |
11 |
Correct |
9 ms |
21080 KB |
Output is correct |
12 |
Correct |
9 ms |
21084 KB |
Output is correct |
13 |
Correct |
10 ms |
21340 KB |
Output is correct |
14 |
Correct |
10 ms |
21084 KB |
Output is correct |
15 |
Correct |
5 ms |
21084 KB |
Output is correct |
16 |
Correct |
10 ms |
21596 KB |
Output is correct |
17 |
Correct |
8 ms |
21084 KB |
Output is correct |
18 |
Correct |
8 ms |
21084 KB |
Output is correct |
19 |
Correct |
9 ms |
21084 KB |
Output is correct |
20 |
Correct |
9 ms |
21084 KB |
Output is correct |
21 |
Correct |
5 ms |
21084 KB |
Output is correct |
22 |
Correct |
4 ms |
21084 KB |
Output is correct |
23 |
Correct |
9 ms |
21084 KB |
Output is correct |
24 |
Correct |
9 ms |
21084 KB |
Output is correct |
25 |
Correct |
8 ms |
21084 KB |
Output is correct |
26 |
Correct |
8 ms |
21084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10584 KB |
Output is correct |
2 |
Correct |
1 ms |
10584 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10584 KB |
Output is correct |
6 |
Correct |
1 ms |
10584 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
10584 KB |
Output is correct |
10 |
Correct |
2 ms |
10584 KB |
Output is correct |
11 |
Correct |
9 ms |
21080 KB |
Output is correct |
12 |
Correct |
9 ms |
21084 KB |
Output is correct |
13 |
Correct |
10 ms |
21340 KB |
Output is correct |
14 |
Correct |
10 ms |
21084 KB |
Output is correct |
15 |
Correct |
5 ms |
21084 KB |
Output is correct |
16 |
Correct |
10 ms |
21596 KB |
Output is correct |
17 |
Correct |
8 ms |
21084 KB |
Output is correct |
18 |
Correct |
8 ms |
21084 KB |
Output is correct |
19 |
Correct |
9 ms |
21084 KB |
Output is correct |
20 |
Correct |
9 ms |
21084 KB |
Output is correct |
21 |
Correct |
5 ms |
21084 KB |
Output is correct |
22 |
Correct |
4 ms |
21084 KB |
Output is correct |
23 |
Correct |
9 ms |
21084 KB |
Output is correct |
24 |
Correct |
9 ms |
21084 KB |
Output is correct |
25 |
Correct |
8 ms |
21084 KB |
Output is correct |
26 |
Correct |
8 ms |
21084 KB |
Output is correct |
27 |
Correct |
208 ms |
41536 KB |
Output is correct |
28 |
Correct |
296 ms |
42064 KB |
Output is correct |
29 |
Correct |
299 ms |
42956 KB |
Output is correct |
30 |
Correct |
218 ms |
42320 KB |
Output is correct |
31 |
Correct |
55 ms |
41044 KB |
Output is correct |
32 |
Correct |
150 ms |
41112 KB |
Output is correct |
33 |
Correct |
41 ms |
31580 KB |
Output is correct |
34 |
Correct |
87 ms |
41300 KB |
Output is correct |
35 |
Correct |
155 ms |
41500 KB |
Output is correct |
36 |
Correct |
152 ms |
41712 KB |
Output is correct |
37 |
Correct |
79 ms |
41076 KB |
Output is correct |
38 |
Correct |
289 ms |
42128 KB |
Output is correct |
39 |
Correct |
79 ms |
41300 KB |
Output is correct |
40 |
Correct |
50 ms |
41288 KB |
Output is correct |