POJ-2251 Dungeon Master (BFS模板题)
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模板题。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # include<iostream> 2 # include<cstdio> 3 # include<algorithm> 4 # include<cstring> 5 # include<queue> 6 using namespace std; 7 struct node 8 { 9 int x,y,z,t; 10 node(int a,int b,int c,int d):x(a),y(b),z(c),t(d){} 11 bool operator < (const node &a) const { 12 return t>a.t; 13 } 14 }; 15 char p[35][35][35],vis[35][35][35]; 16 int l,r,c; 17 int d[6][3]={{0,0,1},{0,0,-1},{1,0,0},{-1,0,0},{0,1,0},{0,-1,0}}; 18 void bfs(int sx,int sy,int sz) 19 { 20 priority_queue<node>q; 21 memset(vis,0,sizeof(vis)); 22 vis[sx][sy][sz]='1'; 23 q.push(node(sx,sy,sz,0)); 24 while(!q.empty()) 25 { 26 node u=q.top(); 27 q.pop(); 28 if(p[u.x][u.y][u.z]=='E'){ 29 printf("Escaped in %d minute(s).\n",u.t); 30 return ; 31 } 32 for(int i=0;i<6;++i){ 33 int nx=u.x+d[i][0],ny=u.y+d[i][1],nz=u.z+d[i][2]; 34 if(nx>=0&&nx<r&&ny>=0&&ny<c&&nz>=0&&nz<l&&!vis[nx][ny][nz]&&p[nx][ny][nz]!='#'){ 35 vis[nx][ny][nz]='1'; 36 q.push(node(nx,ny,nz,u.t+1)); 37 } 38 } 39 } 40 printf("Trapped!\n"); 41 } 42 int main() 43 { 44 while(scanf("%d%d%d",&l,&r,&c),l+r+c) 45 { 46 int sx,sy,sz; 47 for(int k=0;k<l;++k){ 48 for(int i=0;i<r;++i){ 49 for(int j=0;j<c;++j){ 50 cin>>p[i][j][k]; 51 if(p[i][j][k]=='S') 52 sx=i,sy=j,sz=k; 53 } 54 } 55 } 56 bfs(sx,sy,sz); 57 } 58 return 0; 59 }
代码如下: