poj 2251 Dungeon Master (BFS)

来源:互联网 发布:mac 安装oracle 编辑:程序博客网 时间:2024/06/17 23:48

Dungeon Master
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 28186 Accepted: 11006

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!

题意:一个三维的迷宫,要求从S走大E,求最少走多少步,若走不到输出“Trapped!”。

分析:记住以后迷宫的题目一定要用BFS,不考虑太多,用DFS问题太多,超时。。。无解,贴下BFS代码。里面用到一些剪枝,否则超内存,但是我有些没弄明白,明明走过的路都标记过了,为什么还需要用cnt[ i ][ j ][ k ] 与 step 比较大小。

代码如下:

#include <stdio.h>#include <queue>#define INF 1<<30using namespace std;char map[35][35][35];int dir[6][3]={{0,-1,0},{0,0,1},{0,1,0},{0,0,-1},{1,0,0},{-1,0,0}};int cnt[35][35][35];int sx,sy,sz;int ex,ey,ez;int l,r,c;struct node{int x,y,z;int step;};queue <node>q;void init(){int i,j,k;while(!q.empty())q.pop();for(i=0;i<=l;i++){for(j=0;j<=r;j++)for(k=0;k<=c;k++)cnt[i][j][k]=INF;}}int bfs(node f){q.push(f);while(!q.empty()){node p = q.front();q.pop();if(p.x==ex && p.y==ey && p.z==ez)return p.step;map[p.x][p.y][p.z]='#';for(int k=0;k<6;k++){node temp;temp.x=p.x+dir[k][0];temp.y=p.y+dir[k][1];temp.z=p.z+dir[k][2];temp.step=p.step+1;if(temp.x<=0 || temp.x>l || temp.y<=0 || temp.y>r || temp.z<=0 || temp.z>c)continue;if(map[temp.x][temp.y][temp.z]!='#' && cnt[temp.x][temp.y][temp.z]>temp.step){//cnt减枝,计算如果之前走到该点的步数少,则不再走了 cnt[temp.x][temp.y][temp.z]=temp.step;q.push(temp);}}}return -1;}int main(){int i,j,k;while(scanf("%d %d %d%*c",&l,&r,&c),l||r||c){for(i=1;i<=l;i++){for(j=1;j<=r;j++){scanf("%s",&map[i][j][1]);for(k=1;k<=c;k++){if(map[i][j][k]=='S'){sx=i,sy=j,sz=k;}else if(map[i][j][k]=='E'){ex=i;ey=j;ez=k;}}getchar();}}init();node f;f.x=sx;f.y=sy;f.z=sz;f.step=0;int num = bfs(f);if(num==-1)printf("Trapped!\n");elseprintf("Escaped in %d minute(s).\n",num);}return 0;}



0 0
原创粉丝点击