#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double D;
typedef pair<int,int> P;
#define M 1000000007
#define F first
#define S second
#define PB push_back
#define INF 2500000
struct t{
int r,y,x;
};
int h,w,n,dx[4]={1,0,-1,0},dy[4]={0,1,0,-1},m[505][505],dp[9][9][505][505],ans=INF;
vector<P>g[505][505];
vector<t>q[2500000];
void dfs(int y,int x,int d,int py,int px){
g[y][x].PB(P(py,px));
if(m[y][x]==2)d++;
if(m[y][x]==3)d+=3;
d%=4;
int ny=y+dy[d],nx=x+dx[d];
if(0<=ny&&ny<h&&0<=nx&&nx<w&&m[ny][nx]!=1)dfs(ny,nx,d,py,px);
}
int main(void){
scanf("%d%d%d",&n,&w,&h);
for(int i=0;i<n;i++)for(int j=0;i+j<n;j++)for(int y=0;y<h;y++)for(int x=0;x<w;x++)dp[i][j][y][x]=INF;
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
char c;
scanf(" %c",&c);
if(c=='x')m[i][j]=1;
else if(c=='A')m[i][j]=2;
else if(c=='C')m[i][j]=3;
else if(c!='.'){
int p=c-'1';
dp[p][0][i][j]=0;
}
}
}
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
if(m[i][j]==1)continue;
for(int k=0;k<4;k++){
int ny=i+dy[k],nx=j+dx[k];
if(nx<0||nx>=w||ny<0||ny>=h||m[ny][nx]==1){
dfs(i,j,(k+2)%4,i,j);
}
}
}
}
for(int k=0;k+1<n;k++){
int mn=INF,mx=-1;
for(int l=0;l+k<n;l++){
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
int d=dp[l][k][i][j];
if(d!=INF){
q[d].PB(t{l,i,j});
mn=min(mn,d);
mx=max(mx,d);
}
}
}
}
queue<t>bfs;
while(mn<=mx||!bfs.empty()){
if(bfs.empty()){
for(int i=0;i<q[mn].size();i++)bfs.push(q[mn][i]);
q[mn].clear();
mn++;
}
int r=bfs.front().r,y=bfs.front().y,x=bfs.front().x;
int c=dp[r][k][y][x];
bfs.pop();
if(mn<=mx&&c==mn){
for(int i=0;i<q[mn].size();i++)bfs.push(q[mn][i]);
q[mn].clear();
mn++;
}
for(int i=0;i<g[y][x].size();i++){
int ny=g[y][x][i].F,nx=g[y][x][i].S;
if(dp[r][k][ny][nx]>c+1){
dp[r][k][ny][nx]=c+1;
bfs.push(t{r,ny,nx});
}
}
}
for(int l=0;l+k+1<n;l++){
int r=l+k+1;
for(int e=l;e<r;e++){
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
dp[l][k+1][i][j]=min(dp[l][k+1][i][j],dp[l][e-l][i][j]+dp[e+1][r-e-1][i][j]);
}
}
}
}
}
for(int i=0;i<h;i++)for(int j=0;j<w;j++)ans=min(ans,dp[0][n-1][i][j]);
if(ans==INF)ans=-1;
printf("%d\n",ans);
}
Compilation message
robots.cpp: In function 'int main()':
robots.cpp:69:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<q[mn].size();i++)bfs.push(q[mn][i]);
~^~~~~~~~~~~~~
robots.cpp:77:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<q[mn].size();i++)bfs.push(q[mn][i]);
~^~~~~~~~~~~~~
robots.cpp:81:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[y][x].size();i++){
~^~~~~~~~~~~~~~~
robots.cpp:26: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:31:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf(" %c",&c);
~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
65016 KB |
Output is correct |
2 |
Correct |
71 ms |
65008 KB |
Output is correct |
3 |
Correct |
67 ms |
65144 KB |
Output is correct |
4 |
Correct |
64 ms |
65144 KB |
Output is correct |
5 |
Correct |
75 ms |
65144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
65016 KB |
Output is correct |
2 |
Correct |
71 ms |
65008 KB |
Output is correct |
3 |
Correct |
67 ms |
65144 KB |
Output is correct |
4 |
Correct |
64 ms |
65144 KB |
Output is correct |
5 |
Correct |
75 ms |
65144 KB |
Output is correct |
6 |
Correct |
61 ms |
65048 KB |
Output is correct |
7 |
Correct |
71 ms |
65008 KB |
Output is correct |
8 |
Correct |
75 ms |
65220 KB |
Output is correct |
9 |
Correct |
74 ms |
65056 KB |
Output is correct |
10 |
Correct |
73 ms |
65276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
65016 KB |
Output is correct |
2 |
Correct |
71 ms |
65008 KB |
Output is correct |
3 |
Correct |
67 ms |
65144 KB |
Output is correct |
4 |
Correct |
64 ms |
65144 KB |
Output is correct |
5 |
Correct |
75 ms |
65144 KB |
Output is correct |
6 |
Correct |
61 ms |
65048 KB |
Output is correct |
7 |
Correct |
71 ms |
65008 KB |
Output is correct |
8 |
Correct |
75 ms |
65220 KB |
Output is correct |
9 |
Correct |
74 ms |
65056 KB |
Output is correct |
10 |
Correct |
73 ms |
65276 KB |
Output is correct |
11 |
Runtime error |
299 ms |
164864 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
65016 KB |
Output is correct |
2 |
Correct |
71 ms |
65008 KB |
Output is correct |
3 |
Correct |
67 ms |
65144 KB |
Output is correct |
4 |
Correct |
64 ms |
65144 KB |
Output is correct |
5 |
Correct |
75 ms |
65144 KB |
Output is correct |
6 |
Correct |
61 ms |
65048 KB |
Output is correct |
7 |
Correct |
71 ms |
65008 KB |
Output is correct |
8 |
Correct |
75 ms |
65220 KB |
Output is correct |
9 |
Correct |
74 ms |
65056 KB |
Output is correct |
10 |
Correct |
73 ms |
65276 KB |
Output is correct |
11 |
Runtime error |
299 ms |
164864 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
12 |
Halted |
0 ms |
0 KB |
- |