Submission #12716

#TimeUsernameProblemLanguageResultExecution timeMemory
12716baneling100Robots (APIO13_robots)C++98
60 / 100
1500 ms113576 KiB
#include <stdio.h> #include <vector> #include <queue> #define INF 0x7fffffff using namespace std; typedef pair <int,int> ppair; typedef pair <int,ppair> pppair; priority_queue <pppair,vector<pppair>,greater<pppair> > Q; int N, W, H, Ynext[501][501][4], Xnext[501][501][4], Yloc[10], Xloc[10]; int Ydir[4]={0,1,0,-1}, Xdir[4]={1,0,-1,0}, D[10][10][501][501], Ans=INF; int Check[501][501][4]; char Room[501][502]; void DFS(int y, int x, int dir) { int ty, tx, tdir=dir; if(Check[y][x][dir]) return; if(Room[y][x]=='A') { tdir=dir-1; if(tdir<0) tdir=3; } else if(Room[y][x]=='C') { tdir=dir+1; tdir%=4; } ty=y+Ydir[tdir]; tx=x+Xdir[tdir]; if(!(ty>=1 && ty<=H && tx>=1 && tx<=W) || Room[ty][tx]=='x') { Ynext[y][x][dir]=y; Xnext[y][x][dir]=x; } else { DFS(ty,tx,tdir); Ynext[y][x][dir]=Ynext[ty][tx][tdir]; Xnext[y][x][dir]=Xnext[ty][tx][tdir]; } Check[y][x][dir]=1; } void renew(int p, int q) { int i, j, cost, y, x; for(i=1 ; i<=H ; i++) for(j=1 ; j<=W ; j++) if(D[p][q][i][j]!=INF) Q.push(make_pair(D[p][q][i][j],make_pair(i,j))); while(!Q.empty()) { cost=Q.top().first; y=Q.top().second.first; x=Q.top().second.second; Q.pop(); if(cost!=D[p][q][y][x]) continue; for(i=0 ; i<=3 ; i++) if(D[p][q][Ynext[y][x][i]][Xnext[y][x][i]]>D[p][q][y][x]+1) { D[p][q][Ynext[y][x][i]][Xnext[y][x][i]]=D[p][q][y][x]+1; Q.push(make_pair(D[p][q][Ynext[y][x][i]][Xnext[y][x][i]],make_pair(Ynext[y][x][i],Xnext[y][x][i]))); } } } int main(void) { int i, j, k, l, y, x, dir, py, px; scanf("%d %d %d",&N,&W,&H); for(i=1 ; i<=H ; i++) { scanf("%s",&Room[i][1]); for(j=1 ; j<=W ; j++) if('1'<=Room[i][j] && Room[i][j]<='9') { Yloc[Room[i][j]-'0']=i; Xloc[Room[i][j]-'0']=j; Room[i][j]='.'; } } for(i=1 ; i<=H ; i++) for(j=1 ; j<=W ; j++) if(Room[i][j]!='x') for(k=0 ; k<=3 ; k++) DFS(i,j,k); for(k=1 ; k<=N ; k++) { for(i=1 ; i<=H ; i++) for(j=1 ; j<=W ; j++) D[k][k][i][j]=INF; D[k][k][Yloc[k]][Xloc[k]]=0; renew(k,k); } for(i=1 ; i<=N-1 ; i++) for(j=i+1 ; j<=N ; j++) for(y=1 ; y<=H ; y++) for(x=1 ; x<=W ; x++) D[i][j][y][x]=INF; for(l=1 ; l<=N-1 ; l++) for(i=1 ; i<=N-l ; i++) { j=i+l; for(k=i ; k<=j-1 ; k++) for(y=1 ; y<=H ; y++) for(x=1 ; x<=W ; x++) if(D[i][k][y][x]!=INF && D[k+1][j][y][x]!=INF && D[i][j][y][x]>D[i][k][y][x]+D[k+1][j][y][x]) D[i][j][y][x]=D[i][k][y][x]+D[k+1][j][y][x]; renew(i,j); } for(i=1 ; i<=H ; i++) for(j=1 ; j<=W ; j++) if(Ans>D[1][N][i][j]) Ans=D[1][N][i][j]; if(Ans==INF) printf("-1"); else printf("%d",Ans); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...