#include <ctype.h>
#include <stdio.h>
#include <string.h>
#define N 500
#define M 500
#define K 9
#define N_ (N * M * K * K)
#define INF 0x3f3f3f3f
int di[] = { -1, 0, 1, 0 };
int dj[] = { 0, 1, 0, -1 };
char cc[N][M + 1]; int dp[N][M][K], n, m, k;
int solve(int i, int j, int h) {
if (dp[i][j][h] == -1) {
int h_, i_, j_;
dp[i][j][h] = -2;
if (cc[i][j] == 'C')
h_ = (h + 1) % 4;
else if (cc[i][j] == 'A')
h_ = (h + 3) % 4;
else
h_ = h;
i_ = i + di[h_], j_ = j + dj[h_];
if (i_ >= 0 && i_ < n && j_ >= 0 && j_ < m && cc[i_][j_] != 'x')
dp[i][j][h] = solve(i_, j_, h_);
else
dp[i][j][h] = i * m + j;
}
return dp[i][j][h];
}
int iq[1 + N_], pq[N_], dd[N_], cnt;
int lt(int i, int j) {
return dd[i] < dd[j];
}
int p2(int p) {
return (p *= 2) > cnt ? 0 : (p < cnt && lt(iq[p + 1], iq[p]) ? p + 1 : p);
}
void pq_up(int i) {
int p, q, j;
for (p = pq[i]; (q = p / 2) && lt(i, j = iq[q]); p = q)
iq[pq[j] = p] = j;
iq[pq[i] = p] = i;
}
void pq_dn(int i) {
int p, q, j;
for (p = pq[i]; (q = p2(p)) && lt(j = iq[q], i); p = q)
iq[pq[j] = p] = j;
iq[pq[i] = p] = i;
}
void pq_add_last(int i) {
iq[pq[i] = ++cnt] = i;
}
int pq_remove_first() {
int i = iq[1], j = iq[cnt--];
if (j != i)
pq[j] = 1, pq_dn(j);
return i;
}
int main() {
int k, h, i, j, l, r;
scanf("%d%d%d", &k, &m, &n);
for (i = 0; i < n; i++)
scanf("%s", cc[i]);
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
for (h = 0; h < 4; h++)
dp[i][j][h] = -1;
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
for (h = 0; h < 4; h++)
dp[i][j][h] = cc[i][j] == 'x' ? -2 : solve(i, j, h);
memset(dd, 0x3f, n * m * k * k * sizeof *dd);
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
if (isdigit(cc[i][j])) {
int a, u;
a = cc[i][j] - '1';
u = ((i * m + j) * k + a) * k + a;
dd[u] = 0, pq_add_last(u);
}
while (cnt) {
int u = pq_remove_first(), v, ij, d, l_, r_;
ij = u / k / k, i = ij / m, j = ij % m, l = u / k % k, r = u % k;
if (l == 0 && r == k - 1) {
printf("%d\n", dd[u]);
return 0;
}
for (r_ = r + 1; r_ < k; r_++) {
v = (ij * k + l) * k + r_, d = dd[u] + dd[(ij * k + r + 1) * k + r_];
if (dd[v] > d) {
if (dd[v] == INF)
pq_add_last(v);
dd[v] = d, pq_up(v);
}
}
for (l_ = l - 1; l_ >= 0; l_--) {
v = (ij * k + l_) * k + r, d = dd[(ij * k + l_) * k + l - 1] + dd[u];
if (dd[v] > d) {
if (dd[v] == INF)
pq_add_last(v);
dd[v] = d, pq_up(v);
}
}
for (h = 0; h < 4; h++)
if (dp[i][j][h] != -2) {
v = (dp[i][j][h] * k + l) * k + r, d = dd[u] + 1;
if (dd[v] > d) {
if (dd[v] == INF)
pq_add_last(v);
dd[v] = d, pq_up(v);
}
}
}
printf("-1\n");
return 0;
}
Compilation message
robots.c: In function 'main':
robots.c:77:2: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
77 | scanf("%d%d%d", &k, &m, &n);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
robots.c:79:3: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
79 | scanf("%s", cc[i]);
| ^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
0 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
0 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
583 ms |
52904 KB |
Output is correct |
12 |
Correct |
6 ms |
5228 KB |
Output is correct |
13 |
Correct |
17 ms |
22380 KB |
Output is correct |
14 |
Execution timed out |
1569 ms |
55428 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
0 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
583 ms |
52904 KB |
Output is correct |
12 |
Correct |
6 ms |
5228 KB |
Output is correct |
13 |
Correct |
17 ms |
22380 KB |
Output is correct |
14 |
Execution timed out |
1569 ms |
55428 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |