Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 22581 | Accepted: 8814 |
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,把4个方向改为6个方向就行了。
代码如下:
#include<stdio.h> #include<queue> #define INF 0x3f3f3f3f using namespace std; struct stu{ int z,x,y,t; }; stu edge[33*33*33]; char mark[32][32][32]; int dis[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}}; int ans; int l,r,c; bool check(stu temp){ if(temp.z<0||temp.x<0||temp.y<0||temp.z>=l||temp.x>=r||temp.y>=c)return false; if(mark[temp.z][temp.x][temp.y]=='#')return false; return true; } void bfs(int z,int x,int y){ queue<stu>q; while(!q.empty()){ q.pop(); } stu temp,star; temp.z=z;temp.x=x;temp.y=y;temp.t=0; q.push(temp); while(!q.empty()){ star=q.front(); q.pop(); for(int i=1;i<=6;i++){//定义6个方向 temp.x = star.x+dis[i][0]; temp.y = star.y+dis[i][1]; temp.z =star.z+dis[i][2]; temp.t=star.t +1; if(check(temp)){ if(mark[temp.z][temp.x][temp.y]=='E'){ ans=temp.t; return ; } q.push(temp); mark[temp.z][temp.x][temp.y]='#'; } } } } int main(){ int z,x,y; while(scanf("%d%d%d",&l,&r,&c)){ if(l==0&&r==0&&c==0) break; for(int k=0;k<l;k++){ if(k>0)getchar(); for(int i=0;i<r;i++){ getchar(); for(int j=0;j<c;j++){ scanf("%c",&mark[k][i][j]); if(mark[k][i][j]=='S'){ z=k;x=i;y=j; } } } } ans=INF; mark[z][x][y]='#'; bfs(z,x,y); if(ans>=INF){ printf("Trapped!\n"); } else printf("Escaped in %d minute(s).\n",ans); } return 0; }