标签:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 18245 | Accepted: 7075 |
Description
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!
题意:在三维空间上找最短路,其实就是bfs,和二维没有区别,水题
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<queue> using namespace std; const int maxn=50; const int INF=(1<<28); int X,Y,Z; char ch[maxn][maxn][maxn]; bool vis[maxn][maxn][maxn]; int sx,sy,sz; int ans; int dx[]={-1,1,0,0,0,0}; int dy[]={0,0,-1,1,0,0}; int dz[]={0,0,0,0,-1,1}; struct node { int x,y,z; int dist; }; bool bfs() { queue<node> q; q.push({sx,sy,sz,0}); vis[sx][sy][sz]=1; while(!q.empty()){ node now=q.front(); q.pop(); for(int i=0;i<6;i++){ int nx=now.x+dx[i]; int ny=now.y+dy[i]; int nz=now.z+dz[i]; int nd=now.dist+1; if(ch[nx][ny][nz]==‘E‘){ ans=nd; return true; } if(ch[nx][ny][nz]==‘#‘) continue; if(vis[nx][ny][nz]) continue; vis[nx][ny][nz]=1; q.push({nx,ny,nz,nd}); } } return false; } int main() { while(cin>>X>>Y>>Z,X||Y||Z){ memset(ch,‘#‘,sizeof(ch)); for(int i=1;i<=X;i++){ for(int j=1;j<=Y;j++){ for(int k=1;k<=Z;k++){ cin>>ch[i][j][k]; if(ch[i][j][k]==‘S‘){ sx=i; sy=j; sz=k; } } } } ans=0; memset(vis,0,sizeof(vis)); if(bfs()) printf("Escaped in %d minute(s).\n",ans); else printf("Trapped!\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/--560/p/4334129.html