Input
Output
Escaped in x minute(s).
Trapped!
Sample Input
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Sample Output
Escaped in 11 minute(s). Trapped!
代码:
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #include<queue> #include<stack> #include<set> #include<map> #include<vector> #include<cmath> const int maxn=1e5+5; typedef long long ll; char Map[35][35][35]; int vis[35][35][35]; struct node { int x,y,z; int cnt; }; using namespace std; int sx,sy,sz; int ex,ey,ez; int dir[6][3]={{0,0,1},{0,0,-1},{-1,0,0},{1,0,0},{0,1,0},{0,-1,0}}; int s; bool check(int x,int y,int z) { if((Map[z][x][y]==‘.‘||Map[z][x][y]==‘E‘)&&vis[z][x][y]==0) { return true; } else { return false; } } bool bfs() { node start; start.x=sx; start.y=sy; start.z=sz; start.cnt=0; queue<node>q; q.push(start); vis[sz][sx][sy]=1; while(!q.empty()) { node now=q.front(); q.pop(); //printf("%d %d %d\n",now.z,now.x,now.y); if(now.z==ez&&now.x==ex&&now.y==ey) { s=now.cnt; return true; } for(int t=0;t<6;t++) { node next; next.x=now.x+dir[t][0]; next.y=now.y+dir[t][1]; next.z=now.z+dir[t][2]; next.cnt=now.cnt+1; if(check(next.x,next.y,next.z)) { vis[next.z][next.x][next.y]=1; q.push(next); } } } return false; } int main() { int L,R,C; while(scanf("%d%d%d",&L,&R,&C)!=EOF) { memset(vis,0,sizeof(vis)); if(L==0&&R==0&&C==0) { break; } s=0; for(int t=0;t<L;t++) { for(int j=0;j<R;j++) { scanf("%s",Map[t][j]); } } for(int t=0;t<L;t++) { for(int j=0;j<R;j++) { for(int k=0;k<C;k++) { if(Map[t][j][k]==‘S‘) { sx=j; sy=k; sz=t; } if(Map[t][j][k]==‘E‘) { ex=j; ey=k; ez=t; } } } } if(bfs()) printf("Escaped in %d minute(s).\n",s); else { printf("Trapped!\n"); } } return 0; }
原文:https://www.cnblogs.com/Staceyacm/p/10823712.html