POJ - 2251 Dungeon Master

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 41886   Accepted: 15859

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 5
S....
.###.
.##..
###.#

#####
#####
##.##
##...

#####
#####
#.###
####E

1 3 3
S##
#E#
###

0 0 0

Sample Output

Escaped in 11 minute(s).
Trapped!

题目大意:

你被困在3D地牢里,需要找到最快捷的出路!地牢是由单位立方体组成,可能会或可能不会充满岩石。将一个单位向北,南,东,西,向上或向下移动需要一分钟时间。你不能沿着对角线移动,迷宫四面都是坚硬的岩石。 

逃生是否可能?如果是的话,需要多长时间? 

解题思路:用bfs广搜,代码标记很详细,不在赘述。

#include <iostream>
#include "stdio.h"
#include "string.h"
#include "queue"
using namespace std;
#define N 35
char map[N][N][N];//存入地图
int mark[N][N][N];//
int l,r,c;//记录3d迷宫的长 宽 高
int i,j,k;//for循环变量
int si,sj,sk;//储存起始点的坐标
int ei,ej,ek;//储存终止点的坐标
int dx[6]={1,-1,0,0,0,0};//
int dy[6]={0,0,1,-1,0,0};//6种行走方向
int dz[6]={0,0,0,0,1,-1};//
struct ac{
    int x,y,z,step;//储存实时的坐标及步数
};
int judge(int x,int y,int z){//判断条件,过界,是否为路可走;
    if (x>=0&&x<l&&y>=0&&y<r&&z>=0&&z<c&&map[x][y][z]!='#')
        return 1;
    return 0;
}
int bfs(int x,int y,int z){//bfs宽度(层次)搜索,借用队列
    int i,di,dj,dk;
    queue<ac>qu;
    ac cur,next;//cur 记录当前步数  next 记录变化后的坐标
    cur.x=x;
    cur.y=y;
    cur.z=z;
    cur.step=0;//初始化步数
    qu.push(cur);//向队列中存入初始坐标,第一个点
    mark[x][y][z]=1;//将第一个坐标标记;
    while (!qu.empty()) {//判断队列是否为空
        cur=qu.front();//记录当前初始点
        qu.pop();//清空当前队列
        if (cur.x==ei&&cur.y==ej&&cur.z==ek)//判断是否结束,结束返回步数
            return cur.step;
            for (i=0; i<6; i++) {//尝试6种方向
                next.x=di=cur.x+dx[i];
                next.y=dj=cur.y+dy[i];
                next.z=dk=cur.z+dz[i];
                next.step=cur.step+1;
                if(judge(di,dj,dk)&&!mark[di][dj][dk]) {//判断是否过界(judge)是否走过;
                    qu.push(next);//向队列中存入变化坐标
                    mark[di][dj][dk]=1;//标记走过的点
            }
        }
    }
    return -1;//不能逃脱,返回-1
}
 
 
int main() {
    
    while (scanf("%d%d%d",&l,&r,&c),l||r||c) {//输入及输入结束条件
        for (i=0; i<l; i++)
            for (j=0; j<r; j++) {
                scanf("%s",map[i][j]);//记录地图
                for (k=0; k<c; k++) {
                    if (map[i][j][k]=='S') {
                        si=i;sj=j;sk=k;//记录起始点
                    }else if(map[i][j][k]=='E'){
                        ei=i;ej=j;ek=k;//记录终止点
                    }
                }
            }
        memset(mark, 0, sizeof(mark));//标记数组初始化,(队列初始化);
        int ans=bfs(si,sj,sk);
        if (ans==-1) cout<<"Trapped!\n";
        else printf("Escaped in %d minute(s).\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/henu111/article/details/81163904