poj 2251 三维BFS 无坑点

来源:互联网 发布:stata软件和spss 编辑:程序博客网 时间:2024/06/07 01:55
#include<iostream>#include<cstring>#include<cstdio>#include<queue>using namespace std;char map[35][35][35];bool vis[35][35][35];int t,r,c;int dirx[6]={ 1,-1, 0, 0, 0, 0};  #多加两个dirint diry[6]={ 0, 0, 1,-1, 0, 0};int dirz[6]={ 0, 0, 0, 0, 1,-1};struct loc{int x,y,z;int step;};int main(){while(scanf("%d%d%d",&t,&r,&c)==3){if(t==0 && r==0 && c==0) break;int sx,sy,sz;memset(map,0,sizeof(map));memset(vis,0,sizeof(vis));for(int i=1;i<=t;i++){for(int j=1;j<=r;j++){for(int k=1;k<=c;k++){cin >> map[i][j][k];if(map[i][j][k]=='S'){sx=i;sy=j;sz=k;}}}}//cout << 1 << endl;queue<loc> q;loc point;point.x=sx;point.y=sy;point.z=sz;point.step=0;//cout << "*"<< point.x <<","<<point.y<<","<<point.z<<endl;q.push(point);int ans=0;while(!q.empty()){if(ans) break;loc tmp=q.front();q.pop();for(int i=0;i<6;i++){loc nextstep=tmp;nextstep.x+=dirx[i];nextstep.y+=diry[i];nextstep.z+=dirz[i];//cout << nextstep.x <<','<<nextstep.y<<','<<nextstep.z<<endl;if(vis[nextstep.x][nextstep.y][nextstep.z]) continue;if(nextstep.x<1 || nextstep.x>t) continue;if(nextstep.y<1 || nextstep.y>r) continue;if(nextstep.z<1 || nextstep.z>c) continue;if(map[nextstep.x][nextstep.y][nextstep.z]=='#') continue;if(map[nextstep.x][nextstep.y][nextstep.z]=='E'){ans=nextstep.step+1;break;}vis[nextstep.x][nextstep.y][nextstep.z]=1;if(ans) break;nextstep.step++;q.push(nextstep);}}if(ans){printf("Escaped in %d minute(s).\n",ans);}else {printf("Trapped!\n");}}}

原创粉丝点击