#include "bits/stdc++.h"
using namespace std;
using pii = pair <int, int>;
const int dx[] = {0, -1, 0, 1};
const int dy[] = {1, 0, -1, 0};
pii dp[4][505][505];
bool vis[4][505][505];
char s[505][505];
int n, m;
int opt[10][10][505][505];
const int inf = 1e9;
struct data {
int x, y, cost;
data (int x, int y, int cost) : x(x), y(y), cost(cost) {}
data () {}
bool operator < (data d) const {
return cost > d.cost;
}
};
bool inside(int x, int y) {
return (0 <= x && x < n) && (0 <= y && y < m);
}
pii func(int d, int x, int y) {
if(vis[d][x][y] == true) return dp[d][x][y];
vis[d][x][y] = true;
int nd = d;
if(s[x][y] == 'A') nd = (d + 1) % 4;
if(s[x][y] == 'C') nd = (d + 3) % 4;
int nx = x + dx[nd];
int ny = y + dy[nd];
if(inside(nx, ny) && s[nx][ny] != 'x') {
dp[d][x][y] = func(nd, nx, ny);
} else {
dp[d][x][y] = pii(x, y);
}
return dp[d][x][y];
}
void propagate(int l, int r) {
priority_queue <data> Q;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
Q.emplace(i, j, opt[l][r][i][j]);
}
}
while(!Q.empty()) {
data p = Q.top();
Q.pop();
for(int d = 0; d < 4; d++) {
pii q = func(d, p.x, p.y);
if(q.first == -1) continue;
int &ret = opt[l][r][q.first][q.second];
if(ret > opt[l][r][p.x][p.y] + 1) {
ret = opt[l][r][p.x][p.y] + 1;
Q.emplace(q.first, q.second, ret);
}
}
}
return ;
}
int main(int argc, char const *argv[])
{
int N;
scanf("%d %d %d", &N, &n, &m);
swap(n, m);
for(int i = 0; i < n; i++) {
scanf("%s", s[i]);
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
for(int d = 0; d < 4; d++) {
dp[d][i][j] = pii(-1, -1);
vis[d][i][j] = false;
}
}
}
for(int i = 1; i <= N; i++) {
for(int j = i; j <= N; j++) {
for(int x = 0; x < n; x++) {
for(int y = 0; y < m; y++) {
opt[i][j][x][y] = inf;
}
}
}
}
for(int x = 0; x < n; x++) {
for(int y = 0; y < m; y++) {
if(isdigit(s[x][y])) {
int c = s[x][y] - '0';
opt[c][c][x][y] = 0;
propagate(c, c);
}
}
}
for(int sz = 2; sz <= N; sz++) {
for(int i = 1; i <= N - sz + 1; i++) {
int j = i + sz - 1;
for(int x = 0; x < n; x++) {
for(int y = 0; y < m; y++) {
for(int k = i; k < j; k++) {
opt[i][j][x][y] = min(opt[i][j][x][y], opt[i][k][x][y] + opt[k + 1][j][x][y]);
}
}
}
propagate(i, j);
}
}
int ans = inf;
for(int x = 0; x < n; x++) for(int y = 0; y < m; y++) ans = min(ans, opt[1][N][x][y]);
if(ans == inf) ans = -1;
printf("%d\n", ans);
return 0;
}
Compilation message
robots.cpp: In function 'int main(int, const char**)':
robots.cpp:65:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &N, &n, &m);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
robots.cpp:68:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s", s[i]);
~~~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
640 KB |
Output is correct |
5 |
Correct |
5 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
640 KB |
Output is correct |
5 |
Correct |
5 ms |
640 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
640 KB |
Output is correct |
5 |
Correct |
5 ms |
640 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
640 KB |
Output is correct |
11 |
Correct |
527 ms |
35388 KB |
Output is correct |
12 |
Correct |
26 ms |
8184 KB |
Output is correct |
13 |
Correct |
239 ms |
25336 KB |
Output is correct |
14 |
Correct |
873 ms |
35348 KB |
Output is correct |
15 |
Correct |
450 ms |
35364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
640 KB |
Output is correct |
5 |
Correct |
5 ms |
640 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
640 KB |
Output is correct |
11 |
Correct |
527 ms |
35388 KB |
Output is correct |
12 |
Correct |
26 ms |
8184 KB |
Output is correct |
13 |
Correct |
239 ms |
25336 KB |
Output is correct |
14 |
Correct |
873 ms |
35348 KB |
Output is correct |
15 |
Correct |
450 ms |
35364 KB |
Output is correct |
16 |
Correct |
1121 ms |
60568 KB |
Output is correct |
17 |
Execution timed out |
1584 ms |
60520 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |