POJ2251--Dungeon Master--广搜

来源:互联网 发布:非线性叙事结构,知乎 编辑:程序博客网 时间:2024/04/27 09:52

Description

You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.

Is an escape possible? If yes, how long will it take?

Input

The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.

Output

Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
Escaped in x minute(s).

where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Trapped!

Sample Input

3 4 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0

Sample Output

Escaped in 11 minute(s).Trapped!
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <queue>using namespace std;#define inf 0x3f3f3f3f#define maxn 38char map[maxn][maxn][maxn];int dis[maxn][maxn][maxn];int heng[]={0,0,1,-1,0,0};int zong[]={1,-1,0,0,0,0};int shu[]={0,0,0,0,1,-1};int main(){int l,r,c;while(scanf("%d%d%d",&l,&r,&c)==3&&(l||r||c)){memset(dis,0x3f,sizeof(dis));getchar();char A[100];int sl,sr,sc;for(int i=1;i<=l;i++){for(int j=1;j<=r;j++){for(int k=1;k<=c;k++){map[i][j][k]=getchar();if(map[i][j][k]=='S'){sl=i;sr=j;sc=k;}}getchar();}gets(A);}dis[sl][sr][sc]=0;queue <int> ql;queue <int> qr;queue <int> qc;ql.push(sl);qr.push(sr);qc.push(sc);int tl,tr,tc;while(!ql.empty()){int ll=ql.front();int rr=qr.front();int cc=qc.front();if(map[ll][rr][cc]=='E') break;ql.pop();qr.pop();qc.pop();for(int i=0;i<6;i++){int rr_=rr+heng[i];int cc_=cc+zong[i];int ll_=ll+shu[i];if((ll_>=1&&ll_<=l)&&(rr_>=1&&rr_<=r)&&(cc_>=1&&cc_<=c)){if(map[ll_][rr_][cc_]!='#'){if(dis[ll][rr][cc]+1<dis[ll_][rr_][cc_]){dis[ll_][rr_][cc_]=dis[ll][rr][cc]+1;ql.push(ll_);qr.push(rr_);qc.push(cc_);}if(map[ll_][rr_][cc_]=='E'){tl=ll_;tr=rr_;tc=cc_;}}}}}if(dis[tl][tr][tc]!=inf){printf("Escaped in %d minute(s).\n",dis[tl][tr][tc]);}else printf("Trapped!\n");}return 0;}

原创粉丝点击