poj 2251 Dungeon Master(bfs)
题目:http://poj.org/problem?id=2251
View Code
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #define pan(a,b,c) (a<=b&&b<=c) 5 using namespace std; 6 int dir[6][3]={{0,0,-1},{0,0,1},{-1,0,0},{1,0,0},{0,-1,0},{0,1,0}}; 7 int ex,ey,ez; 8 char str[35][35][35]; 9 int map[35][35][35]; 10 struct node 11 { 12 int x,y,z; 13 int num; 14 }que[30010]; 15 int main() 16 { 17 int m,n,t; 18 int i,j,k; 19 int sx,sy,sz; 20 while(scanf("%d%d%d%*c",&t,&m,&n)!=EOF) 21 { 22 if(t==0&&m==0&&n==0) 23 break; 24 for(i=1;i<=t;i++) 25 { 26 for(j=1;j<=m;j++) 27 { 28 for(k=1;k<=n;k++) 29 { 30 cin>>str[i][j][k]; 31 if(str[i][j][k]=='S') 32 { 33 sx=i; 34 sy=j; 35 sz=k; 36 } 37 } 38 } 39 } 40 memset(map,0,sizeof(map)); 41 int head=0; 42 int tail=1; 43 que[0].x=sx; 44 que[0].y=sy; 45 que[0].z=sz; 46 que[0].num=0; 47 map[sx][sy][sz]=1; 48 int xx,yy,zz; 49 int flag=0; 50 while(head<tail) 51 { 52 53 for(i=0;i<6;i++) 54 { 55 xx=que[head].x+dir[i][0]; 56 yy=que[head].y+dir[i][1]; 57 zz=que[head].z+dir[i][2]; 58 59 if(str[xx][yy][zz]=='E') 60 { 61 printf("Escaped in %d minute(s).\n",que[head].num+1); 62 flag=1; 63 break; 64 } 65 if(pan(1,xx,t)&&pan(1,yy,m)&&pan(1,zz,n)&&map[xx][yy][zz]==0&&str[xx][yy][zz]=='.') 66 { 67 que[tail].x=xx; 68 que[tail].y=yy; 69 que[tail].z=zz; 70 que[tail].num=que[head].num+1; 71 map[xx][yy][zz]=1; 72 tail++; 73 } 74 } 75 if(flag) 76 break; 77 head++; 78 } 79 if(!flag) 80 { 81 puts("Trapped!"); 82 } 83 } 84 return 0; 85 }