#include <bits/stdc++.h>
using namespace std;
const int dr[] = {-1, 0, +1, 0};
const int dc[] = {0, +1, 0, -1};
enum {
STAND = 4,
UP = 0,
RIGHT = 1,
DOWN = 2,
LEFT = 3,
NOT_GOOD = 1000000000
};
int n, m, k;
char a[500][500];
int c[500][500];
int f[9][9][500][500];
int g[4][500][500];
vector<pair<int, int>> adj[500][500];
int DP(int z, int i, int j) {
if (g[z][i][j] != -1) return g[z][i][j];
g[z][i][j] = NOT_GOOD;
int zz = (z + c[i][j]) % 4;
int ii = i + dr[zz];
int jj = j + dc[zz];
if (ii < 0 || ii >= n || jj < 0 || jj >= m || a[ii][jj] == 'x') return g[z][i][j] = i * m + j;
return g[z][i][j] = DP(zz, ii, jj);
}
int DP(int l, int r, int i, int j) {
if (f[l][r][i][j] != -1) return f[l][r][i][j];
f[l][r][i][j] = NOT_GOOD;
for (int m = l; m < r; m++) f[l][r][i][j] = min(f[l][r][i][j], DP(l, m, i, j) + DP(m + 1, r, i, j));
for (auto [ii, jj] : adj[i][j]) f[l][r][i][j] = min(f[l][r][i][j], DP(l, r, ii, jj) + 1);
return f[l][r][i][j];
}
int main() {
memset(f, -1, sizeof f);
cin >> k >> m >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] == 'A') {
c[i][j] = +3;
} else if (a[i][j] == 'C') {
c[i][j] = +1;
} else if (a[i][j] == 'x') {
c[i][j] = -1;
} else if (a[i][j] == '.') {
c[i][j] = 0;
} else {
a[i][j] -= '0' + 1;
assert(0 <= a[i][j] && a[i][j] < k);
f[a[i][j]][a[i][j]][i][j] = 0;
}
}
}
memset(g, -1, sizeof g);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
for (int z = 0; z < 4; z++) {
if (DP(z, i, j) == NOT_GOOD) continue;
int ii = DP(z, i, j) / m;
int jj = DP(z, i, j) % m;
adj[ii][jj].emplace_back(i, j);
}
}
}
int res = NOT_GOOD;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
res = min(res, DP(0, k - 1, i, j));
}
}
cout << (res == NOT_GOOD ? -1 : res);
}
Compilation message
robots.cpp: In function 'int main()':
robots.cpp:58:41: warning: array subscript has type 'char' [-Wchar-subscripts]
58 | f[a[i][j]][a[i][j]][i][j] = 0;
| ~~~~~~^
robots.cpp:58:50: warning: array subscript has type 'char' [-Wchar-subscripts]
58 | f[a[i][j]][a[i][j]][i][j] = 0;
| ~~~~~~^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
89292 KB |
Output is correct |
2 |
Correct |
41 ms |
89268 KB |
Output is correct |
3 |
Correct |
44 ms |
89324 KB |
Output is correct |
4 |
Correct |
51 ms |
89344 KB |
Output is correct |
5 |
Correct |
44 ms |
89340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
89292 KB |
Output is correct |
2 |
Correct |
41 ms |
89268 KB |
Output is correct |
3 |
Correct |
44 ms |
89324 KB |
Output is correct |
4 |
Correct |
51 ms |
89344 KB |
Output is correct |
5 |
Correct |
44 ms |
89340 KB |
Output is correct |
6 |
Correct |
40 ms |
89324 KB |
Output is correct |
7 |
Correct |
47 ms |
89268 KB |
Output is correct |
8 |
Correct |
44 ms |
89320 KB |
Output is correct |
9 |
Correct |
48 ms |
89264 KB |
Output is correct |
10 |
Correct |
49 ms |
89272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
89292 KB |
Output is correct |
2 |
Correct |
41 ms |
89268 KB |
Output is correct |
3 |
Correct |
44 ms |
89324 KB |
Output is correct |
4 |
Correct |
51 ms |
89344 KB |
Output is correct |
5 |
Correct |
44 ms |
89340 KB |
Output is correct |
6 |
Correct |
40 ms |
89324 KB |
Output is correct |
7 |
Correct |
47 ms |
89268 KB |
Output is correct |
8 |
Correct |
44 ms |
89320 KB |
Output is correct |
9 |
Correct |
48 ms |
89264 KB |
Output is correct |
10 |
Correct |
49 ms |
89272 KB |
Output is correct |
11 |
Incorrect |
323 ms |
96664 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
89292 KB |
Output is correct |
2 |
Correct |
41 ms |
89268 KB |
Output is correct |
3 |
Correct |
44 ms |
89324 KB |
Output is correct |
4 |
Correct |
51 ms |
89344 KB |
Output is correct |
5 |
Correct |
44 ms |
89340 KB |
Output is correct |
6 |
Correct |
40 ms |
89324 KB |
Output is correct |
7 |
Correct |
47 ms |
89268 KB |
Output is correct |
8 |
Correct |
44 ms |
89320 KB |
Output is correct |
9 |
Correct |
48 ms |
89264 KB |
Output is correct |
10 |
Correct |
49 ms |
89272 KB |
Output is correct |
11 |
Incorrect |
323 ms |
96664 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |