#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
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 vis[1010][1010];
char in[2000010];
int main() {
fread(in, 1, sizeof(in), stdin);
int T = 0;
for (T=0; in[T] != ' '; T++) n = n * 10 + in[T] - '0';
for (T++; in[T] != '\n'; T++) m = m * 10 + in[T] - '0';
for (int i=1; i<=n; i++) {
for (int j=1; j<=m; j++) {
T ++;
if (in[T] == '#') A[i][j] = 0;
else A[i][j] = 1;
}
T ++;
}
for (T++; in[T] != ' '; T++) sx = sx * 10 + in[T] - '0';
for (T++; in[T] != '\n'; T++) sy = sy * 10 + in[T] - '0';
for (T++; in[T] != ' '; T++) ex = ex * 10 + in[T] - '0';
for (T++; in[T] != '\n'; T++) ey = ey * 10 + in[T] - '0';
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};
}
}
vector<pii> q1, q2, q3;
q1.push_back({sx, sy});
vis[sx][sy] = 1;
int t = 0;
while (!q1.empty() || !q2.empty()) {
for (auto &[x, y] : q1) {
if (x == ex && y == ey) {
printf("%d\n", t);
return 0;
}
for (auto &[nx, ny] : {L[x][y], R[x][y], U[x][y], D[x][y]}) {
if (vis[nx][ny]) continue;
vis[nx][ny] = 1;
q2.push_back({nx, ny});
}
}
for (auto &[x, y] : q1) {
for (int i=0; i<4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (!A[nx][ny]) continue;
if (vis[nx][ny]) continue;
vis[nx][ny] = 1;
q3.push_back({nx, ny});
}
}
q1 = q2;
q2 = q3;
q3.clear();
t ++;
}
printf("-1\n");
}
Compilation message
skating.cpp: In function 'int main()':
skating.cpp:24:10: warning: ignoring return value of 'size_t fread(void*, size_t, size_t, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
24 | fread(in, 1, sizeof(in), stdin);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
1 ms |
12632 KB |
Output is correct |
3 |
Correct |
1 ms |
12636 KB |
Output is correct |
4 |
Correct |
2 ms |
12632 KB |
Output is correct |
5 |
Correct |
1 ms |
12632 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
12636 KB |
Output is correct |
8 |
Correct |
1 ms |
12632 KB |
Output is correct |
9 |
Correct |
2 ms |
12632 KB |
Output is correct |
10 |
Correct |
2 ms |
12632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
1 ms |
12632 KB |
Output is correct |
3 |
Correct |
1 ms |
12636 KB |
Output is correct |
4 |
Correct |
2 ms |
12632 KB |
Output is correct |
5 |
Correct |
1 ms |
12632 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
12636 KB |
Output is correct |
8 |
Correct |
1 ms |
12632 KB |
Output is correct |
9 |
Correct |
2 ms |
12632 KB |
Output is correct |
10 |
Correct |
2 ms |
12632 KB |
Output is correct |
11 |
Correct |
3 ms |
21080 KB |
Output is correct |
12 |
Correct |
4 ms |
21336 KB |
Output is correct |
13 |
Correct |
3 ms |
21592 KB |
Output is correct |
14 |
Correct |
5 ms |
21340 KB |
Output is correct |
15 |
Correct |
3 ms |
20824 KB |
Output is correct |
16 |
Correct |
4 ms |
21596 KB |
Output is correct |
17 |
Correct |
4 ms |
21084 KB |
Output is correct |
18 |
Correct |
4 ms |
21336 KB |
Output is correct |
19 |
Correct |
4 ms |
21336 KB |
Output is correct |
20 |
Correct |
3 ms |
21080 KB |
Output is correct |
21 |
Correct |
3 ms |
21336 KB |
Output is correct |
22 |
Correct |
3 ms |
21336 KB |
Output is correct |
23 |
Correct |
4 ms |
21080 KB |
Output is correct |
24 |
Correct |
4 ms |
21336 KB |
Output is correct |
25 |
Correct |
4 ms |
21080 KB |
Output is correct |
26 |
Correct |
4 ms |
21336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
1 ms |
12632 KB |
Output is correct |
3 |
Correct |
1 ms |
12636 KB |
Output is correct |
4 |
Correct |
2 ms |
12632 KB |
Output is correct |
5 |
Correct |
1 ms |
12632 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
12636 KB |
Output is correct |
8 |
Correct |
1 ms |
12632 KB |
Output is correct |
9 |
Correct |
2 ms |
12632 KB |
Output is correct |
10 |
Correct |
2 ms |
12632 KB |
Output is correct |
11 |
Correct |
3 ms |
21080 KB |
Output is correct |
12 |
Correct |
4 ms |
21336 KB |
Output is correct |
13 |
Correct |
3 ms |
21592 KB |
Output is correct |
14 |
Correct |
5 ms |
21340 KB |
Output is correct |
15 |
Correct |
3 ms |
20824 KB |
Output is correct |
16 |
Correct |
4 ms |
21596 KB |
Output is correct |
17 |
Correct |
4 ms |
21084 KB |
Output is correct |
18 |
Correct |
4 ms |
21336 KB |
Output is correct |
19 |
Correct |
4 ms |
21336 KB |
Output is correct |
20 |
Correct |
3 ms |
21080 KB |
Output is correct |
21 |
Correct |
3 ms |
21336 KB |
Output is correct |
22 |
Correct |
3 ms |
21336 KB |
Output is correct |
23 |
Correct |
4 ms |
21080 KB |
Output is correct |
24 |
Correct |
4 ms |
21336 KB |
Output is correct |
25 |
Correct |
4 ms |
21080 KB |
Output is correct |
26 |
Correct |
4 ms |
21336 KB |
Output is correct |
27 |
Correct |
30 ms |
42584 KB |
Output is correct |
28 |
Correct |
67 ms |
43108 KB |
Output is correct |
29 |
Correct |
13 ms |
42584 KB |
Output is correct |
30 |
Correct |
29 ms |
42788 KB |
Output is correct |
31 |
Correct |
24 ms |
40024 KB |
Output is correct |
32 |
Correct |
39 ms |
42576 KB |
Output is correct |
33 |
Correct |
11 ms |
33368 KB |
Output is correct |
34 |
Correct |
28 ms |
42328 KB |
Output is correct |
35 |
Correct |
37 ms |
42316 KB |
Output is correct |
36 |
Correct |
43 ms |
42328 KB |
Output is correct |
37 |
Correct |
27 ms |
42072 KB |
Output is correct |
38 |
Correct |
68 ms |
43024 KB |
Output is correct |
39 |
Correct |
29 ms |
42072 KB |
Output is correct |
40 |
Correct |
15 ms |
42072 KB |
Output is correct |