#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 503;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
struct data {
int x, y;
data (int _x = 0, int _y = 0) : x(_x), y(_y) {}
} to[N][N][4];
bool vis[N][N][4];
char s[N][N];
int f[10][10][N][N], n, w, h;
data work(int x, int y, int tmp) {
if (vis[x][y][tmp]) return to[x][y][tmp];
vis[x][y][tmp] = true;
if (s[x][y] == 'A') (++tmp) &= 3;
if (s[x][y] == 'C') (--tmp) &= 3;
if (s[x + dx[tmp]][y + dy[tmp]] == 'x')
return data(x, y);
else
return to[x + dx[tmp]][y + dy[tmp]][tmp] = work(x + dx[tmp], y + dy[tmp], tmp);
}
int tot = 0, l, r, id[N * N];
data q1[N * N], q2[N * N], tt;
template <typename T> void check_min(T &a, T b) {if (b < a) a = b;}
template <typename T> void check_max(T &a, T b) {if (b > a) a = b;}
bool cmp(data A, data B) {return f[l][r][A.x][A.y] < f[l][r][B.x][B.y];}
int c[N * N * 20];
void BFS() {
// stable_sort(q1 + 1, q1 + tot + 1, cmp);
int mx = 0, num;
for (int i = 1; i <= tot; ++i) if ((num = f[l][r][q1[i].x][q1[i].y]) != 1010580540) check_max(mx, num);
memset(c, 0, sizeof(int) * (mx + 2));
for (int i = 1; i <= tot; ++i) {
num = f[l][r][q1[i].x][q1[i].y];
if (num != 1010580540) ++c[num];
else ++c[mx + 1];
}
for (int i = 1; i <= mx + 1; ++i) c[i] += c[i - 1];
for (int i = tot; i >= 1; --i) {
num = f[l][r][q1[i].x][q1[i].y];
if (num == 1010580540) num = mx + 1;
id[c[num]--] = i;
}
int head = 1, tail = 0, tmp = 1, x, y, x2, y2;
while (head <= tail || tmp <= tot) {
x = q2[head].x; y = q2[head].y;
x2 = q1[id[tmp]].x; y2 = q1[id[tmp]].y;
if (head <= tail && tmp <= tot && f[l][r][x][y] < f[l][r][x2][y2] || tmp > tot) ++head;
else ++tmp, x = x2, y = y2;
for (int d = 0; d < 4; ++d) {
tt = to[x][y][d];
if (tt.x == 0) continue;
if (f[l][r][tt.x][tt.y] > f[l][r][x][y] + 1) {
f[l][r][tt.x][tt.y] = f[l][r][x][y] + 1;
q2[++tail] = tt;
}
}
}
}
int main() {
scanf("%d%d%d", &n, &w, &h);
memset(f, 60, sizeof(f));
for (int i = 1; i <= h; ++i)
scanf("%s", s[i] + 1);
for (int i = 1; i <= h; ++i) s[i][0] = s[i][w + 1] = 'x';
for (int i = 1; i <= w; ++i) s[0][i] = s[h + 1][i] = 'x';
for (int i = 1 ; i <= h; ++i)
for (int j = 1; j <= w; ++j)
if (s[i][j] != 'x')
for (int dt = 0; dt < 4; ++dt)
to[i][j][dt] = work(i, j, dt);
for (int i = 1; i <= h; ++i)
for (int j = 1; j <= w; ++j) q1[++tot] = data(i, j);
bool flag;
for (int i = n; i >= 1; --i) {
flag = false;
for (int ii = 1; ii <= h; ++ii) {
for (int jj = 1; jj <= w; ++jj)
if (s[ii][jj] == '0' + i) {
f[i][i][ii][jj] = 0;
flag = true;
break;
}
if (flag) break;
}
l = r = i; BFS();
for (int j = i + 1; j <= n; ++j) {
for (int k = i; k < j; ++k)
for (int ii = 1; ii <= h; ++ii)
for (int jj = 1; jj <= w; ++jj)
check_min(f[i][j][ii][jj], f[i][k][ii][jj] + f[k + 1][j][ii][jj]);
l = i; r = j; BFS();
}
}
int ans = 0x7fffffff;
for (int i = 1; i <= h; ++i)
for (int j = 1; j <= w; ++j)
check_min(ans, f[1][n][i][j]);
printf("%d\n", ans == 1010580540 ? -1 : ans);
return 0;
}
Compilation message
robots.cpp: In function 'void BFS()':
robots.cpp:60:34: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
60 | if (head <= tail && tmp <= tot && f[l][r][x][y] < f[l][r][x2][y2] || tmp > tot) ++head;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
robots.cpp: In function 'int main()':
robots.cpp:74:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
74 | scanf("%d%d%d", &n, &w, &h);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
robots.cpp:79:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
79 | scanf("%s", s[i] + 1);
| ~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
111172 KB |
Output is correct |
2 |
Correct |
48 ms |
111084 KB |
Output is correct |
3 |
Correct |
50 ms |
111172 KB |
Output is correct |
4 |
Correct |
50 ms |
111164 KB |
Output is correct |
5 |
Correct |
51 ms |
111120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
111172 KB |
Output is correct |
2 |
Correct |
48 ms |
111084 KB |
Output is correct |
3 |
Correct |
50 ms |
111172 KB |
Output is correct |
4 |
Correct |
50 ms |
111164 KB |
Output is correct |
5 |
Correct |
51 ms |
111120 KB |
Output is correct |
6 |
Correct |
50 ms |
111164 KB |
Output is correct |
7 |
Correct |
53 ms |
111112 KB |
Output is correct |
8 |
Correct |
51 ms |
111172 KB |
Output is correct |
9 |
Correct |
51 ms |
111248 KB |
Output is correct |
10 |
Correct |
50 ms |
111096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
111172 KB |
Output is correct |
2 |
Correct |
48 ms |
111084 KB |
Output is correct |
3 |
Correct |
50 ms |
111172 KB |
Output is correct |
4 |
Correct |
50 ms |
111164 KB |
Output is correct |
5 |
Correct |
51 ms |
111120 KB |
Output is correct |
6 |
Correct |
50 ms |
111164 KB |
Output is correct |
7 |
Correct |
53 ms |
111112 KB |
Output is correct |
8 |
Correct |
51 ms |
111172 KB |
Output is correct |
9 |
Correct |
51 ms |
111248 KB |
Output is correct |
10 |
Correct |
50 ms |
111096 KB |
Output is correct |
11 |
Correct |
199 ms |
112576 KB |
Output is correct |
12 |
Correct |
57 ms |
112272 KB |
Output is correct |
13 |
Correct |
130 ms |
112372 KB |
Output is correct |
14 |
Correct |
253 ms |
112532 KB |
Output is correct |
15 |
Correct |
202 ms |
112448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
111172 KB |
Output is correct |
2 |
Correct |
48 ms |
111084 KB |
Output is correct |
3 |
Correct |
50 ms |
111172 KB |
Output is correct |
4 |
Correct |
50 ms |
111164 KB |
Output is correct |
5 |
Correct |
51 ms |
111120 KB |
Output is correct |
6 |
Correct |
50 ms |
111164 KB |
Output is correct |
7 |
Correct |
53 ms |
111112 KB |
Output is correct |
8 |
Correct |
51 ms |
111172 KB |
Output is correct |
9 |
Correct |
51 ms |
111248 KB |
Output is correct |
10 |
Correct |
50 ms |
111096 KB |
Output is correct |
11 |
Correct |
199 ms |
112576 KB |
Output is correct |
12 |
Correct |
57 ms |
112272 KB |
Output is correct |
13 |
Correct |
130 ms |
112372 KB |
Output is correct |
14 |
Correct |
253 ms |
112532 KB |
Output is correct |
15 |
Correct |
202 ms |
112448 KB |
Output is correct |
16 |
Correct |
452 ms |
113620 KB |
Output is correct |
17 |
Correct |
643 ms |
114380 KB |
Output is correct |
18 |
Correct |
404 ms |
113916 KB |
Output is correct |
19 |
Correct |
409 ms |
113640 KB |
Output is correct |
20 |
Correct |
460 ms |
114148 KB |
Output is correct |