# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
207645 |
2020-03-08T09:13:09 Z |
PeppaPig |
Robots (APIO13_robots) |
C++14 |
|
1500 ms |
52728 KB |
#include <bits/stdc++.h>
#define pii pair<int, int>
#define x first
#define y second
using namespace std;
const int N = 505;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, -1, 0, 1};
struct item {
int r, c, id;
item(int r, int c, int id) : r(r), c(c), id(id) {}
};
int n, w, h, dp[N][N][40];
char A[N][N];
pii nex[N][N][4];
map<pii, int> mp;
pii find_next(int r, int c, int dir) {
if(nex[r][c][dir] == pii(-1, -1) && A[r][c] != 'x') {
int ndir = dir;
if(A[r][c] == 'C') ndir = (dir + 1) % 4;
if(A[r][c] == 'A') ndir = (dir + 3) % 4;
int nr = r + dx[ndir], nc = c + dy[ndir];
if(nr < 1 || nr > h || nc < 1 || nc > w || A[nr][nc] == 'x')
nex[r][c][dir] = pii(r, c);
else nex[r][c][dir] = find_next(nr, nc, ndir);
}
return nex[r][c][dir];
}
int main() {
fill_n(nex[0][0], N * N * 4, pii(-1, -1));
fill_n(dp[0][0], N * N * 40, 1e9);
scanf("%d %d %d", &n, &w, &h);
for(int i = 1; i <= h; i++) scanf(" %s", A[i] + 1);
for(int i = 1; i <= h; i++)
for(int j = 1; j <= w; j++)
for(int k = 0; k < 4; k++)
find_next(i, j, k);
for(int i = 1, ptr = 0; i <= n; i++) for(int j = i; j <= n; j++)
mp[pii(i, j)] = ++ptr;
for(int k = 1; k <= n; k++) {
queue<item> Q;
for(int i = 1; i <= h; i++) for(int j = 1; j <= w; j++) {
if(A[i][j] == 'x') continue;
if(k == 1) {
if('1' <= A[i][j] && A[i][j] <= '9') {
int now = mp[pii(A[i][j] - '0', A[i][j] - '0')];
Q.emplace(i, j, now);
dp[i][j][now] = 0;
}
} else {
for(int x = 1, y = k; y <= n; x++, y++) {
int now = mp[pii(x, y)];
for(int l = x; l < y; l++) {
int L = mp[pii(x, l)], R = mp[pii(l + 1, y)];
dp[i][j][now] = min(dp[i][j][now], dp[i][j][L] + dp[i][j][R]);
}
Q.emplace(i, j, now);
}
}
}
while(!Q.empty()) {
item u = Q.front(); Q.pop();
if(k == 1) {
pii now;
for(auto p : mp) if(p.y == u.id) now = p.x;
// printf("now at %d %d (%d %d)\n", u.r, u.c, now.x, now.y);
// printf("push = %d\n", dp[u.r][u.c][u.id]);
}
for(int i = 0; i < 4; i++) {
int nr = u.r + dx[i], nc = u.c + dy[i];
if(nr < 1 || nr > h || nc < 1 || nc > w || A[nr][nc] == 'x')
continue;
pii v = nex[nr][nc][i];
// if(k == 1) printf("nex %d = (%d %d)", i, v.x, v.y);
if(dp[u.r][u.c][u.id] + 1 < dp[v.x][v.y][u.id]) {
dp[v.x][v.y][u.id] = dp[u.r][u.c][u.id] + 1;
Q.emplace(v.x, v.y, u.id);
}
}
}
}
int ans = 1e9;
for(int i = 1; i <= h; i++) for(int j = 1; j <= w; j++)
ans = min(ans, dp[i][j][mp[pii(1, n)]]);
if(ans != 1e9) printf("%d\n", ans);
else printf("-1\n");
// printf("(%d %d)\n", nex[4][7][2].x, nex[4][7][2].y);
return 0;
}
Compilation message
robots.cpp: In function 'int main()':
robots.cpp:40:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &n, &w, &h);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
robots.cpp:41:38: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i = 1; i <= h; i++) scanf(" %s", A[i] + 1);
~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
48248 KB |
Output is correct |
2 |
Correct |
36 ms |
48248 KB |
Output is correct |
3 |
Correct |
32 ms |
48248 KB |
Output is correct |
4 |
Correct |
32 ms |
48248 KB |
Output is correct |
5 |
Correct |
33 ms |
48248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
48248 KB |
Output is correct |
2 |
Correct |
36 ms |
48248 KB |
Output is correct |
3 |
Correct |
32 ms |
48248 KB |
Output is correct |
4 |
Correct |
32 ms |
48248 KB |
Output is correct |
5 |
Correct |
33 ms |
48248 KB |
Output is correct |
6 |
Correct |
32 ms |
48248 KB |
Output is correct |
7 |
Correct |
34 ms |
48248 KB |
Output is correct |
8 |
Correct |
33 ms |
48248 KB |
Output is correct |
9 |
Correct |
32 ms |
48248 KB |
Output is correct |
10 |
Correct |
32 ms |
48248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
48248 KB |
Output is correct |
2 |
Correct |
36 ms |
48248 KB |
Output is correct |
3 |
Correct |
32 ms |
48248 KB |
Output is correct |
4 |
Correct |
32 ms |
48248 KB |
Output is correct |
5 |
Correct |
33 ms |
48248 KB |
Output is correct |
6 |
Correct |
32 ms |
48248 KB |
Output is correct |
7 |
Correct |
34 ms |
48248 KB |
Output is correct |
8 |
Correct |
33 ms |
48248 KB |
Output is correct |
9 |
Correct |
32 ms |
48248 KB |
Output is correct |
10 |
Correct |
32 ms |
48248 KB |
Output is correct |
11 |
Execution timed out |
1591 ms |
52728 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
48248 KB |
Output is correct |
2 |
Correct |
36 ms |
48248 KB |
Output is correct |
3 |
Correct |
32 ms |
48248 KB |
Output is correct |
4 |
Correct |
32 ms |
48248 KB |
Output is correct |
5 |
Correct |
33 ms |
48248 KB |
Output is correct |
6 |
Correct |
32 ms |
48248 KB |
Output is correct |
7 |
Correct |
34 ms |
48248 KB |
Output is correct |
8 |
Correct |
33 ms |
48248 KB |
Output is correct |
9 |
Correct |
32 ms |
48248 KB |
Output is correct |
10 |
Correct |
32 ms |
48248 KB |
Output is correct |
11 |
Execution timed out |
1591 ms |
52728 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |