poj 22515 Dungeon Master

来源:互联网 发布:逃不开的经济周期2淘宝 编辑:程序博客网 时间:2024/06/06 16:25
Dungeon Master
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 20926 Accepted: 8110

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!
bfs:
 
代码:
 
#include<stdio.h>#include<string.h>#include<queue>#include<algorithm>using namespace std;struct record{int x,y,z;    //坐标; int time;    //时间; };int vis[35][35][35];char s[35][35][35];    int dx[6]={-1,0,0,1,0,0};  //六个方向; int dy[6]={0,-1,0,0,1,0};int dz[6]={0,0,-1,0,0,1};int l,r,c;int x1,y1,z1;queue<record>q;int judge(record e)  //判断该位置是否符合条件; {if(e.x<0||e.x>=l||e.y<0||e.y>=r||e.z<0||e.z>=c||vis[e.x][e.y][e.z]||s[e.x][e.y][e.z]=='#'){return 0;}return 1;}void bfs(){    record e,temp;    int i;while(!q.empty())   //清空队列; {q.pop();}vis[x1][y1][z1]=1;  //标记为被访问过; e.x=x1;e.y=y1;e.z=z1;e.time=0;    //从(x1,y1,z1)开始,时间首先定为0; q.push(e);   //将可以走的位置插入到队列中; while(!q.empty()) //如果队列不为空; {e=q.front();  //e设为队列的第一个元素; q.pop();  //取出第一个元素 ; for(i=0;i<6;i++)  //六个方向进行搜索; {temp=e;temp.x+=dx[i];temp.y+=dy[i];temp.z+=dz[i];temp.time=e.time+1;  //时间+1; if(!judge(temp))  //不满足条件跳出本次循环 {    continue;    }if(s[temp.x][temp.y][temp.z]=='E') //遇到出口输出; {printf("Escaped in %d minute(s).\n",temp.time);    return ;}vis[temp.x][temp.y][temp.z]=1;//标记该位置已访问; q.push(temp);  }}printf("Trapped!\n");}int main(){int i,j,k,key;while(scanf("%d%d%d",&l,&r,&c)!=EOF&&l||r||c){memset(vis,0,sizeof(vis));for(i=0;i<l;i++){for(j=0;j<r;j++){scanf("%s",s[i][j]);}}for(i=0;i<l;i++){for(j=0;j<r;j++){for(k=0;k<c;k++){if(s[i][j][k]=='S'){x1=i;y1=j;z1=k;}}}}    bfs();}return 0;}

0 0
原创粉丝点击