# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
696181 |
2023-02-05T17:36:25 Z |
rainboy |
None (JOI16_skating) |
C |
|
109 ms |
24716 KB |
#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 nxt[4][N][M], 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--;
for (i = 0; i < n; i++)
for (j = 0; j < m; j++) {
nxt[0][i][j] = cc[i][j] == '#' ? (i + 1) * m + j : nxt[0][i - 1][j];
nxt[2][i][j] = cc[i][j] == '#' ? i * m + (j + 1) : nxt[2][i][j - 1];
}
for (i = n - 1; i >= 0; i--)
for (j = m - 1; j >= 0; j--) {
nxt[1][i][j] = cc[i][j] == '#' ? (i - 1) * m + j : nxt[1][i + 1][j];
nxt[3][i][j] = cc[i][j] == '#' ? i * m + (j - 1) : nxt[3][i][j + 1];
}
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];
if (cc[i_][j_] == '#')
continue;
ij_ = nxt[g][i][j];
if (dd[ij_] > d + 1)
dd[ij_] = d + 1, qu1[cnt1++] = ij_;
ij_ = i_ * m + j_;
if (dd[ij_] > d + 2)
dd[ij_] = d + 2, qu2[cnt2++] = ij_;
}
}
printf("-1\n");
return 0;
}
Compilation message
skating.c: In function 'main':
skating.c:34:32: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
34 | 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--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
0 ms |
468 KB |
Output is correct |
4 |
Correct |
0 ms |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
0 ms |
468 KB |
Output is correct |
4 |
Correct |
0 ms |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
468 KB |
Output is correct |
11 |
Correct |
3 ms |
3796 KB |
Output is correct |
12 |
Correct |
3 ms |
3924 KB |
Output is correct |
13 |
Correct |
2 ms |
3796 KB |
Output is correct |
14 |
Correct |
3 ms |
3796 KB |
Output is correct |
15 |
Correct |
2 ms |
3796 KB |
Output is correct |
16 |
Correct |
3 ms |
3924 KB |
Output is correct |
17 |
Correct |
4 ms |
3864 KB |
Output is correct |
18 |
Correct |
3 ms |
3796 KB |
Output is correct |
19 |
Correct |
3 ms |
3924 KB |
Output is correct |
20 |
Correct |
2 ms |
3668 KB |
Output is correct |
21 |
Correct |
3 ms |
3796 KB |
Output is correct |
22 |
Correct |
2 ms |
3796 KB |
Output is correct |
23 |
Correct |
3 ms |
3924 KB |
Output is correct |
24 |
Correct |
3 ms |
3924 KB |
Output is correct |
25 |
Correct |
3 ms |
3796 KB |
Output is correct |
26 |
Correct |
5 ms |
3796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
0 ms |
468 KB |
Output is correct |
4 |
Correct |
0 ms |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
468 KB |
Output is correct |
11 |
Correct |
3 ms |
3796 KB |
Output is correct |
12 |
Correct |
3 ms |
3924 KB |
Output is correct |
13 |
Correct |
2 ms |
3796 KB |
Output is correct |
14 |
Correct |
3 ms |
3796 KB |
Output is correct |
15 |
Correct |
2 ms |
3796 KB |
Output is correct |
16 |
Correct |
3 ms |
3924 KB |
Output is correct |
17 |
Correct |
4 ms |
3864 KB |
Output is correct |
18 |
Correct |
3 ms |
3796 KB |
Output is correct |
19 |
Correct |
3 ms |
3924 KB |
Output is correct |
20 |
Correct |
2 ms |
3668 KB |
Output is correct |
21 |
Correct |
3 ms |
3796 KB |
Output is correct |
22 |
Correct |
2 ms |
3796 KB |
Output is correct |
23 |
Correct |
3 ms |
3924 KB |
Output is correct |
24 |
Correct |
3 ms |
3924 KB |
Output is correct |
25 |
Correct |
3 ms |
3796 KB |
Output is correct |
26 |
Correct |
5 ms |
3796 KB |
Output is correct |
27 |
Correct |
53 ms |
22788 KB |
Output is correct |
28 |
Correct |
96 ms |
24456 KB |
Output is correct |
29 |
Correct |
16 ms |
20948 KB |
Output is correct |
30 |
Correct |
33 ms |
22228 KB |
Output is correct |
31 |
Correct |
21 ms |
20792 KB |
Output is correct |
32 |
Correct |
53 ms |
24716 KB |
Output is correct |
33 |
Correct |
11 ms |
10068 KB |
Output is correct |
34 |
Correct |
30 ms |
21964 KB |
Output is correct |
35 |
Correct |
48 ms |
23592 KB |
Output is correct |
36 |
Correct |
46 ms |
23328 KB |
Output is correct |
37 |
Correct |
29 ms |
21524 KB |
Output is correct |
38 |
Correct |
109 ms |
24616 KB |
Output is correct |
39 |
Correct |
44 ms |
23372 KB |
Output is correct |
40 |
Correct |
21 ms |
22100 KB |
Output is correct |