#include <bits/stdc++.h>
using namespace std;
const int mxN=9, mxS=500, mxD=mxS*mxS*mxN;
int n, w, h, di[4]={-1, 0, 1, 0}, dj[4]={0, 1, 0, -1}, d[mxN*(mxN+1)/2][mxS][mxS], ans=INT_MAX, m1[mxN][mxN];
string g[mxS];
array<int, 2> e[mxS][mxS][4], m2[mxN*(mxN+1)/2];
vector<array<int, 2>> bd[mxD];
bool ib(int i, int j) {
return i<0||j<0||i>=h||j>=w||g[i][j]=='x';
}
array<int, 2> dfs(int i, int j, int k) {
if(e[i][j][k][0]==-1) {
e[i][j][k][0]=-2;
int nk=k;
if(g[i][j]=='C')
nk=(k+1)%4;
else if(g[i][j]=='A')
nk=(k+3)%4;
int ni=i+di[nk], nj=j+dj[nk];
e[i][j][k]=ib(ni, nj)?array<int, 2>{i, j}:dfs(ni, nj, nk);
}
return e[i][j][k];
}
void bfs(int d[mxS][mxS]) {
memset(d, 0x3f, sizeof(::d[0]));
for(int di=0; di<mxD; ++di) {
for(array<int, 2> u : bd[di]) {
if(d[u[0]][u[1]]<=di)
continue;
d[u[0]][u[1]]=di;
for(int i=0; i<4; ++i)
if(e[u[0]][u[1]][i][0]!=-2)
bd[di+1].push_back(e[u[0]][u[1]][i]);
}
bd[di].clear();
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> w >> h;
for(int i=0; i<h; ++i)
cin >> g[i];
memset(e, -1, sizeof(e));
for(int i=0; i<h; ++i)
for(int j=0; j<w; ++j)
for(int k=0; k<4; ++k)
dfs(i, j, k);
for(int i=0, k=0; i<n; ++i) {
for(int j=i; j>=0; --j, ++k) {
m1[j][i]=k;
m2[k]={j, i};
if(j<i) {
for(int x=0; x<h; ++x)
for(int y=0; y<w; ++y)
for(int l=j; l<i; ++l)
if(d[m1[j][l]][x][y]+d[m1[l+1][i]][x][y]<mxD)
bd[d[m1[j][l]][x][y]+d[m1[l+1][i]][x][y]].push_back({x, y});
} else
for(int x=0; x<h; ++x)
for(int y=0; y<w; ++y)
if(g[x][y]-'0'-1==j)
bd[0].push_back({x, y});
bfs(d[k]);
}
}
for(int i=0; i<h; ++i)
for(int j=0; j<w; ++j)
ans=min(d[m1[0][n-1]][i][j], ans);
cout << (ans>1e9?-1:ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
90 ms |
63992 KB |
Output is correct |
2 |
Correct |
89 ms |
63992 KB |
Output is correct |
3 |
Correct |
90 ms |
63992 KB |
Output is correct |
4 |
Correct |
70 ms |
63992 KB |
Output is correct |
5 |
Correct |
72 ms |
63992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
90 ms |
63992 KB |
Output is correct |
2 |
Correct |
89 ms |
63992 KB |
Output is correct |
3 |
Correct |
90 ms |
63992 KB |
Output is correct |
4 |
Correct |
70 ms |
63992 KB |
Output is correct |
5 |
Correct |
72 ms |
63992 KB |
Output is correct |
6 |
Correct |
79 ms |
64008 KB |
Output is correct |
7 |
Correct |
84 ms |
64036 KB |
Output is correct |
8 |
Correct |
86 ms |
64120 KB |
Output is correct |
9 |
Correct |
83 ms |
63992 KB |
Output is correct |
10 |
Correct |
70 ms |
63992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
90 ms |
63992 KB |
Output is correct |
2 |
Correct |
89 ms |
63992 KB |
Output is correct |
3 |
Correct |
90 ms |
63992 KB |
Output is correct |
4 |
Correct |
70 ms |
63992 KB |
Output is correct |
5 |
Correct |
72 ms |
63992 KB |
Output is correct |
6 |
Correct |
79 ms |
64008 KB |
Output is correct |
7 |
Correct |
84 ms |
64036 KB |
Output is correct |
8 |
Correct |
86 ms |
64120 KB |
Output is correct |
9 |
Correct |
83 ms |
63992 KB |
Output is correct |
10 |
Correct |
70 ms |
63992 KB |
Output is correct |
11 |
Correct |
575 ms |
125464 KB |
Output is correct |
12 |
Correct |
64 ms |
63480 KB |
Output is correct |
13 |
Correct |
345 ms |
88796 KB |
Output is correct |
14 |
Correct |
952 ms |
143836 KB |
Output is correct |
15 |
Correct |
572 ms |
109732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
90 ms |
63992 KB |
Output is correct |
2 |
Correct |
89 ms |
63992 KB |
Output is correct |
3 |
Correct |
90 ms |
63992 KB |
Output is correct |
4 |
Correct |
70 ms |
63992 KB |
Output is correct |
5 |
Correct |
72 ms |
63992 KB |
Output is correct |
6 |
Correct |
79 ms |
64008 KB |
Output is correct |
7 |
Correct |
84 ms |
64036 KB |
Output is correct |
8 |
Correct |
86 ms |
64120 KB |
Output is correct |
9 |
Correct |
83 ms |
63992 KB |
Output is correct |
10 |
Correct |
70 ms |
63992 KB |
Output is correct |
11 |
Correct |
575 ms |
125464 KB |
Output is correct |
12 |
Correct |
64 ms |
63480 KB |
Output is correct |
13 |
Correct |
345 ms |
88796 KB |
Output is correct |
14 |
Correct |
952 ms |
143836 KB |
Output is correct |
15 |
Correct |
572 ms |
109732 KB |
Output is correct |
16 |
Correct |
678 ms |
105748 KB |
Output is correct |
17 |
Runtime error |
852 ms |
164864 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
18 |
Halted |
0 ms |
0 KB |
- |