1. 程式人生 > 其它 >1248:Dungeon Master

1248:Dungeon Master

Dungeon Master

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<queue>
 5 using namespace std;
 6 const int N=105;
 7 
 8 int l,r,c,a[N][N][N],t[]={-1,1,0,0,0,0,0,0,-1,1,0,0,0,0,0,0,1,-1};
 9 char cc[N][N][N];
10 queue<int> q;
11 void bfs(){
12     while(!q.empty()){
13 int z=q.front(); 14 q.pop(); 15 int x=q.front(); 16 q.pop(); 17 int y=q.front(); 18 q.pop(); 19 for(int i=0;i<6;i++){ 20 int ni=x+t[i],nj=y+t[i+6],nk=z+t[i+12]; 21 if(ni>0&&ni<=r&&nj>0&&nj<=c&&nk>0
&&nk<=l&&cc[nk][ni][nj]!='#'&&(a[nk][ni][nj]>a[z][x][y]+1||!a[nk][ni][nj])){ 22 q.push(nk),q.push(ni),q.push(nj); 23 a[nk][ni][nj]=a[z][x][y]+1; 24 } 25 } 26 } 27 } 28 int main(){ 29 int x,y,z; 30 while(cin>>l>>r>>c){
31 getchar();//接收換行符 32 if(!l&&!r&&!c)break; 33 for(int i=1;i<=l;i++) 34 for(int j=1;j<=r;j++){ 35 for(int k=1;k<=c;k++){ 36 scanf("%c",&cc[i][j][k]); 37 if(cc[i][j][k]=='S') 38 q.push(i),q.push(j),q.push(k); 39 if(cc[i][j][k]=='E') 40 z=i,x=j,y=k; 41 } 42 getchar(); 43 } 44 memset(a,0,sizeof(a)); 45 bfs(); 46 if(a[z][x][y])printf("Escaped in %d minute(s).\n",a[z][x][y]); 47 else printf("Trapped!\n"); 48 } 49 return 0; 50 }