Escaped in x minute(s).
Trapped!
AC-code:
#include<cstdio> #include<cstring> #include<queue> #define INF 0xffffff int ans,l,r,c,ex,ey,ez,vis[35][35][35]; char a[35][35][35]; int dx[6]={0,0,0,0,1,-1}; int dy[6]={1,-1,0,0,0,0}; int dz[6]={0,0,1,-1,0,0}; using namespace std; struct node { int x,y,z,step; friend bool operator < (node a,node b) { return a.step>b.step; } }temp,p; void bfs(int x1,int y1,int z1) { p.x=x1; p.y=y1; p.z=z1; p.step=0; vis[p.x][p.y][p.z]=1; priority_queue<node>q; q.push(p); while(!q.empty()) { p=q.top(); q.pop(); for(int i=0;i<6;i++) { temp.x=p.x+dx[i]; temp.y=p.y+dy[i]; temp.z=p.z+dz[i]; temp.step=p.step+1; if(!vis[temp.x][temp.y][temp.z]&&temp.x>=0&&temp.x<l&&temp.y>=0&&temp.y<r&&temp.z>=0&&temp.z<c) { if(temp.x==ex&&temp.y==ey&&temp.z==ez) { printf("Escaped in %d minute(s).\n",temp.step); return ; } vis[temp.x][temp.y][temp.z]=1; q.push(temp); } } } printf("Trapped!\n"); } int main() { int i,j,k,x,y,z; while(scanf("%d%d%d",&l,&r,&c),l||r||c) { memset(vis,0,sizeof(vis)); for(i=0;i<l;i++,getchar()) { for(j=0;j<r;j++) { getchar(); for(k=0;k<c;k++) { scanf("%c",&a[i][j][k]); if(a[i][j][k]=='S') { x=i; y=j; z=k; } else if(a[i][j][k]=='E') { ex=i; ey=j; ez=k; } else if(a[i][j][k]=='#') vis[i][j][k]=1; else vis[i][j][k]=0; } } } ans=INF; bfs(x,y,z); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/lin14543/article/details/47319711