#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <cassert>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P2;
#define rep(i, n) for (int i=0; i<(n); i++)
#define pb push_back
#define all(x) x.begin(), x.end()
#define uniq(x) x.erase(unique(all(x)), x.end())
#define INF 1000001919
#define _1 first
#define _2 second
const int DX[4] = {-1, 0, 1, 0};
const int DY[4] = {0, -1, 0, 1};
int N, W, H;
char A[500][500];
P dst[500][500][4];
P f(int x, int y, int d) {
if (dst[x][y][d] != P(-2, -2)) return dst[x][y][d];
dst[x][y][d] = P(-1, -1);
int nx = x+DX[d], ny = y+DY[d], nd = d;
if (nx<0||nx>=W||ny<0||ny>=H||A[nx][ny]=='x') return dst[x][y][d] = P(x, y);
if (A[nx][ny] == 'A') nd = (d+3)%4;
if (A[nx][ny] == 'C') nd = (d+1)%4;
return dst[x][y][d] = f(nx, ny, nd);
}
//const int MAX_D = 10*500*500;
//vector<P> Q[MAX_D];
void bfs(int D[500][500]) {
//rep(i, MAX_D) Q[i].clear();
priority_queue<P2, vector<P2>, greater<P2> > q;
rep(x, W) rep(y, H) if (D[x][y] != INF) q.push(P2(D[x][y], P(x, y)));
while (!q.empty()) {
int r = q.top()._1;
P cur = q.top()._2;
q.pop();
if (D[cur._1][cur._2] < r) continue;
rep(k, 4) {
P np = dst[cur._1][cur._2][k];
if (np._1 != -1 && D[np._1][np._2] > r+1) {
D[np._1][np._2] = r+1;
q.push(P2(r+1, np));
}
}
}
}
int X[9], Y[9];
int dp[9][9][500][500];
int main() {
cin >> N >> W >> H;
rep(y, H) rep(x, W) {
cin >> A[x][y];
if ('0' <= A[x][y] && A[x][y] <= '9') X[A[x][y]-'1'] = x, Y[A[x][y]-'1'] = y;
}
rep(x, W) rep(y, H) rep(d, 4) dst[x][y][d] = P(-2, -2);
rep(x, W) rep(y, H) if (A[x][y] != 'x') rep(d, 4) f(x, y, d);
rep(i, N) rep(j, N) rep(x, W) rep(y, H) dp[i][j][x][y] = INF;
rep(i, N) dp[i][i][X[i]][Y[i]] = 0;
for (int len=1; len<=N; len++) {
rep(l, N) {
int r = l+len-1;
if (r >= N) break;
rep(x, W) rep(y, H) for (int k=l; k<r; k++) {
dp[l][r][x][y] = min(dp[l][r][x][y], dp[l][k][x][y]+dp[k+1][r][x][y]);
}
bfs(dp[l][r]);
}
}
int m = INF;
rep(x, W) rep(y, H) m = min(m, dp[0][N-1][x][y]);
if (m == INF) m = -1;
cout << m << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
376 KB |
Output is correct |
2 |
Correct |
1 ms |
480 KB |
Output is correct |
3 |
Correct |
1 ms |
560 KB |
Output is correct |
4 |
Correct |
1 ms |
628 KB |
Output is correct |
5 |
Correct |
1 ms |
672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
376 KB |
Output is correct |
2 |
Correct |
1 ms |
480 KB |
Output is correct |
3 |
Correct |
1 ms |
560 KB |
Output is correct |
4 |
Correct |
1 ms |
628 KB |
Output is correct |
5 |
Correct |
1 ms |
672 KB |
Output is correct |
6 |
Correct |
2 ms |
672 KB |
Output is correct |
7 |
Correct |
1 ms |
672 KB |
Output is correct |
8 |
Correct |
1 ms |
672 KB |
Output is correct |
9 |
Correct |
1 ms |
672 KB |
Output is correct |
10 |
Correct |
1 ms |
712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
376 KB |
Output is correct |
2 |
Correct |
1 ms |
480 KB |
Output is correct |
3 |
Correct |
1 ms |
560 KB |
Output is correct |
4 |
Correct |
1 ms |
628 KB |
Output is correct |
5 |
Correct |
1 ms |
672 KB |
Output is correct |
6 |
Correct |
2 ms |
672 KB |
Output is correct |
7 |
Correct |
1 ms |
672 KB |
Output is correct |
8 |
Correct |
1 ms |
672 KB |
Output is correct |
9 |
Correct |
1 ms |
672 KB |
Output is correct |
10 |
Correct |
1 ms |
712 KB |
Output is correct |
11 |
Correct |
190 ms |
52580 KB |
Output is correct |
12 |
Correct |
16 ms |
52580 KB |
Output is correct |
13 |
Correct |
53 ms |
52580 KB |
Output is correct |
14 |
Correct |
765 ms |
53208 KB |
Output is correct |
15 |
Correct |
164 ms |
53208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
376 KB |
Output is correct |
2 |
Correct |
1 ms |
480 KB |
Output is correct |
3 |
Correct |
1 ms |
560 KB |
Output is correct |
4 |
Correct |
1 ms |
628 KB |
Output is correct |
5 |
Correct |
1 ms |
672 KB |
Output is correct |
6 |
Correct |
2 ms |
672 KB |
Output is correct |
7 |
Correct |
1 ms |
672 KB |
Output is correct |
8 |
Correct |
1 ms |
672 KB |
Output is correct |
9 |
Correct |
1 ms |
672 KB |
Output is correct |
10 |
Correct |
1 ms |
712 KB |
Output is correct |
11 |
Correct |
190 ms |
52580 KB |
Output is correct |
12 |
Correct |
16 ms |
52580 KB |
Output is correct |
13 |
Correct |
53 ms |
52580 KB |
Output is correct |
14 |
Correct |
765 ms |
53208 KB |
Output is correct |
15 |
Correct |
164 ms |
53208 KB |
Output is correct |
16 |
Correct |
190 ms |
88452 KB |
Output is correct |
17 |
Execution timed out |
1560 ms |
91360 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |