标签:
Escaped in x minute(s).
Trapped!
1 #include<stdio.h> 2 #include<queue> 3 using namespace std; 4 //const int INF=0xfffffff; 5 struct Node{ 6 int nx,ny,nz,step; 7 friend bool operator < (Node a,Node b){ 8 return a.step>b.step; 9 } 10 }; 11 Node a,b; 12 char map[40][40][40]; 13 int vis[40][40][40]; 14 int disx[6]={0, 0, 1,-1,0, 0}; 15 int disy[6]={1,-1, 0, 0,0, 0}; 16 int disz[6]={0, 0, 0, 0,1,-1}; 17 int sx,sy,sz,ex,ey,ez,minstep,flot,L,R,C; 18 priority_queue<Node>dl; 19 bool judge(Node s){ 20 if(s.nx<0||s.nx>=R)return false; 21 if(s.ny<0||s.ny>=C)return false; 22 if(s.nz<0||s.nz>=L)return false; 23 if(vis[s.nz][s.nx][s.ny])return false;//写错了,找了半天。。。 24 return true; 25 } 26 void bfs(){ 27 a.nx=sx;a.ny=sy;a.nz=sz;a.step=0; 28 dl.push(a); 29 while(!dl.empty()){ 30 a=dl.top(); 31 dl.pop(); 32 vis[a.nz][a.nx][a.ny]=1; 33 if(a.nx==ex&&a.ny==ey&&a.nz==ez){ 34 minstep=a.step; 35 flot=1; 36 //if(a.step<minstep)minstep=a.step; 37 return; 38 } 39 for(int i=0;i<6;i++){ 40 b.nx=a.nx+disx[i];b.ny=a.ny+disy[i];b.nz=a.nz+disz[i];b.step=a.step+1; 41 if(!judge(b))continue; 42 if(map[b.nz][b.nx][b.ny]==‘.‘||map[b.nz][b.nx][b.ny]==‘E‘)dl.push(b); 43 } 44 } 45 } 46 int main(){int x,y,z; 47 while(~scanf("%d%d%d",&L,&R,&C),L||R||C){flot=0;//minstep=INF; 48 while(!dl.empty())dl.pop(); 49 for(z=0;z<L;z++)for(x=0;x<R;x++)scanf("%s",map[z][x]); 50 for(z=0;z<L;z++)for(x=0;x<R;x++){//printf("%s\n",map[z][x]); 51 for(y=0;y<C;y++){ 52 if(map[z][x][y]==‘S‘)sx=x,sy=y,sz=z; 53 if(map[z][x][y]==‘E‘)ex=x,ey=y,ez=z; 54 } 55 } 56 //printf("%d %d %d %d %d %d\n",sx,sy,sz,ex,ey,ez); 57 bfs(); 58 if(flot){ 59 printf("Escaped in %d minute(s).\n",minstep); 60 } 61 else puts("Trapped!"); 62 } 63 return 0; 64 }
标签:
原文地址:http://www.cnblogs.com/handsomecui/p/4706227.html