POJ 2251 Dungeon Master

来源:互联网 发布:qq群加人软件 编辑:程序博客网 时间:2024/06/05 10:55

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!

题目大意:给定一个3D的区域,只允许向上下左右前后方向移动,每次耗时1S,求从起点至重点最小用时,若无法移动至重点输出Trapped。


题目思路:宽搜,用结构体封装三维坐标,将起点加入队列,取出队列里一个坐标,利用常量数组遍历六个方向
若遍历到的坐标未被访问过则将其加入队列,记录该坐标时间为现坐标时间+1,当队列为空或抵达终点搜索结束
#include <stdio.h>
#inclde  <iostream>#include <queue>char a[35][35][35];int l,r,c,xx;int X[6]={1,-1,0,0,0,0};int Y[6]={0,0,1,-1,0,0};int Z[6]={0,0,0,0,1,-1};using namespace std;struct node{    int x;    int y;    int z;}point;queue<node>f;int YJ(int x,int y,int z){    if(a[x][y][z]=='#') return 1;    if(x<1||x>l) return 1;    if(y<1||y>r) return 1;    if(z<1||z>c) return 1;    return 0;}int main(){    int k,q,i,k1,q1,i1,t;    while(~scanf("%d %d %d",&l,&r,&c))    {        int b[35][35][35]={0};        xx=0;        if(l==0&&r==0&&c==0) break;        for(i=1;i<=l;i++)        {            if(i!=1) getchar();            for(k=1;k<=r;k++)            {                getchar();                for(q=1;q<=c;q++) {scanf("%c",&a[i][k][q]);if(a[i][k][q]=='S') {i1=i;k1=k;q1=q;}}            }        }        if(a[i1][k1][q1]=='E') {printf("0\n");continue;}        point.x=i1;point.y=k1;point.z=q1;b[i1][k1][q1]=1;f.push(point);        while(!f.empty())        {            point=f.front();f.pop();            if(a[point.x][point.y][point.z]=='E') {xx=1;break;}            for(t=0;t<=5;t++)            {                if(b[point.x+X[t]][point.y+Y[t]][point.z+Z[t]]==0&&YJ(point.x+X[t],point.y+Y[t],point.z+Z[t])==0)                {                    i1=point.x;k1=point.y;q1=point.z;                    b[point.x+X[t]][point.y+Y[t]][point.z+Z[t]]=b[point.x][point.y][point.z]+1;                    point.x=point.x+X[t];point.y=point.y+Y[t];point.z=point.z+Z[t];                    f.push(point);                    point.x=i1;point.y=k1;point.z=q1;                }            }        }        xx==0?printf("Trapped!\n"):printf("Escaped in %d minute(s).\n",b[point.x][point.y][point.z]-1);        while(!f.empty()) f.pop();    }    return 0;}
原创粉丝点击