#include <stdio.h>
#include <string.h>
#define N 1000
#define M 1000
int di[] = { -1, 1, 0, 0 };
int dj[] = { 0, 0, -1, 1 };
int main() {
static char cc[N][M + 1];
static int dd[N * M], qu1[N * M], qu2[N * M];
int n, m, cnt1, cnt2, g, h1, h2, is, js, it, jt, i, j, i_, j_, ij, ij_, d;
scanf("%d%d", &n, &m);
for (i = 0; i < n; i++)
scanf("%s", cc[i]);
scanf("%d%d%d%d", &is, &js, &it, &jt), is--, js--, it--, jt--;
memset(dd, 0x3f, n * m * sizeof *dd);
cnt1 = cnt2 = 0, h1 = h2 = 0;
ij = is * m + js;
dd[ij] = 0, qu1[cnt1++] = ij;
while (h1 < cnt1 || h2 < cnt2) {
ij = h2 == cnt2 || h1 < cnt1 && dd[qu1[h1]] < dd[qu2[h2]] ? qu1[h1++] : qu2[h2++], i = ij / m, j = ij % m, d = dd[ij];
if (i == it && j == jt) {
printf("%d\n", d);
return 0;
}
for (g = 0; g < 4; g++) {
i_ = i + di[g], j_ = j + dj[g], ij_ = i_ * m + j_;
if (cc[i_][j_] == '#')
continue;
if (dd[ij_] > d + 2)
dd[ij_] = d + 2, qu2[cnt2++] = ij_;
while (cc[i_][j_] != '#')
i_ += di[g], j_ += dj[g];
i_ -= di[g], j_ -= dj[g];
ij_ = i_ * m + j_;
if (dd[ij_] > d + 1)
dd[ij_] = d + 1, qu1[cnt1++] = ij_;
}
}
printf("-1\n");
return 0;
}
Compilation message
skating.c: In function 'main':
skating.c:24:32: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
24 | ij = h2 == cnt2 || h1 < cnt1 && dd[qu1[h1]] < dd[qu2[h2]] ? qu1[h1++] : qu2[h2++], i = ij / m, j = ij % m, d = dd[ij];
| ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
skating.c:15:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
15 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
skating.c:17:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | scanf("%s", cc[i]);
| ^~~~~~~~~~~~~~~~~~
skating.c:18:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
18 | scanf("%d%d%d%d", &is, &js, &it, &jt), is--, js--, it--, jt--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
8 ms |
724 KB |
Output is correct |
12 |
Correct |
9 ms |
820 KB |
Output is correct |
13 |
Correct |
3 ms |
724 KB |
Output is correct |
14 |
Correct |
4 ms |
696 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
7 ms |
820 KB |
Output is correct |
17 |
Correct |
4 ms |
724 KB |
Output is correct |
18 |
Correct |
3 ms |
724 KB |
Output is correct |
19 |
Correct |
4 ms |
724 KB |
Output is correct |
20 |
Correct |
4 ms |
724 KB |
Output is correct |
21 |
Correct |
2 ms |
912 KB |
Output is correct |
22 |
Correct |
3 ms |
704 KB |
Output is correct |
23 |
Correct |
4 ms |
724 KB |
Output is correct |
24 |
Correct |
6 ms |
852 KB |
Output is correct |
25 |
Correct |
3 ms |
724 KB |
Output is correct |
26 |
Correct |
4 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
8 ms |
724 KB |
Output is correct |
12 |
Correct |
9 ms |
820 KB |
Output is correct |
13 |
Correct |
3 ms |
724 KB |
Output is correct |
14 |
Correct |
4 ms |
696 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
7 ms |
820 KB |
Output is correct |
17 |
Correct |
4 ms |
724 KB |
Output is correct |
18 |
Correct |
3 ms |
724 KB |
Output is correct |
19 |
Correct |
4 ms |
724 KB |
Output is correct |
20 |
Correct |
4 ms |
724 KB |
Output is correct |
21 |
Correct |
2 ms |
912 KB |
Output is correct |
22 |
Correct |
3 ms |
704 KB |
Output is correct |
23 |
Correct |
4 ms |
724 KB |
Output is correct |
24 |
Correct |
6 ms |
852 KB |
Output is correct |
25 |
Correct |
3 ms |
724 KB |
Output is correct |
26 |
Correct |
4 ms |
724 KB |
Output is correct |
27 |
Correct |
752 ms |
8100 KB |
Output is correct |
28 |
Correct |
1187 ms |
9752 KB |
Output is correct |
29 |
Correct |
11 ms |
6228 KB |
Output is correct |
30 |
Correct |
48 ms |
7596 KB |
Output is correct |
31 |
Correct |
5 ms |
6136 KB |
Output is correct |
32 |
Correct |
1530 ms |
10160 KB |
Output is correct |
33 |
Correct |
17 ms |
2472 KB |
Output is correct |
34 |
Correct |
41 ms |
7256 KB |
Output is correct |
35 |
Correct |
224 ms |
8964 KB |
Output is correct |
36 |
Correct |
76 ms |
8688 KB |
Output is correct |
37 |
Correct |
28 ms |
6948 KB |
Output is correct |
38 |
Correct |
473 ms |
9932 KB |
Output is correct |
39 |
Correct |
40 ms |
11304 KB |
Output is correct |
40 |
Correct |
194 ms |
7360 KB |
Output is correct |