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?
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.
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
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
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 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Sample Output
Escaped in 11 minute(s). Trapped!
最简单BFS,直接代码:
#include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<iostream> #include<algorithm> #include<vector> #include<map> #include<queue> #include<stack> #include<string> #include<map> #include<set> #include<ctime> #define eps 1e-6 #define MAX 100005 #define INF 0x3f3f3f3f #define LL long long #define pii pair<int,int> #define rd(x) scanf("%d",&x) #define rd2(x,y) scanf("%d%d",&x,&y) #define rd3(x,y,z) scanf("%d%d%d",&x,&y,&z) ///map<int,int>mmap; ///map<int,int >::iterator it; using namespace std; int dir[6][3]={{-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,1},{0,0,-1}}; int level,row,col; char mmap[35][35][35]; bool vis[35][35][35]; struct Pos { int l; int r; int c; int coun; Pos(){} Pos(int l,int r,int c,int coun) { this->l=l,this->r=r,this->c=c,this->coun = coun; } }start,eend; bool Ok(int l,int r,int c){ return (mmap[l][r][c]=='.' && l>=0 && r>=0 && c>=0 && l<level && r<row && c<col && vis[l][r][c]==false); } int main () { while(~scanf("%d%d%d",&level,&row,&col)&&level!=0&&row!=0&&col!=0) { memset(vis,0,sizeof(vis)); for(int i=0; i<level; i++) for(int j=0; j<row; j++) { scanf("%s",mmap[i][j]); for(int k=0; k<col; k++) { if(mmap[i][j][k] == 'S') start.l=i,start.r = j,start.c = k,start.coun=0; if(mmap[i][j][k] == 'E') eend.l=i,eend.r = j,eend.c = k,mmap[i][j][k] = '.'; } } queue<Pos> que; que.push(start); vis[start.l][start.r-1][start.c]=true; int coun = -1,come = 0; while(!que.empty()) { Pos a=que.front(); que.pop(); if(a.l==eend.l&&a.r==eend.r&&a.c==eend.c) { come = 1; coun = a.coun; break; } for(int i=0;i<6;i++){ int l=a.l+dir[i][0],r=a.r+dir[i][1],c=a.c+dir[i][2]; if( Ok(l,r,c) ) { que.push(Pos(l,r,c,a.coun+1)); vis[l][r][c]=1; } } } if(come) printf("Escaped in %d minute(s).\n",coun); else printf("Trapped!\n"); } return 0; }