POJ 2251 Dungeon Master 三维空间BFS(基础题)

B - Dungeon Master

  POJ - 2251 

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!

题意:

给你一个地牢的深L,长C,宽R,每移动一个单位需要1s,.代表可以通过,#代表不可以通过,问从出发点S能不能到达终点E,如果能到达就输出逃出地牢所需的秒数,如果不能逃出就输出Trapped。

思路:

典型的dfs找到起点然后分别向前后左右上下六个方向分别遍历如果能通过的话更新起点再遍历前后左右上下四个方向知道找到终点E位置。如果找不到终点E输出Trapped,因为题中是一个3D立体图,所以我们要定义三位数组代表x,y,z轴。

代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>

using namespace std;

struct node
{
    int x;
    int y;
    int z;
    int step;
};

int dir[6][3]= {{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};//前后左右上下六个方向坐标
char maze[50][50][50]; //迷宫
int vis[50][50][50];   //表示是否访问过
int l,r,c; //l表示层数 r表示行数 c表示列数
int sx,sy,sz;  //起点坐标
int ex,ey,ez;

int check(int z,int x,int y) //判断该点是否可以走
{
    if(x>=0&&x<r&&y>=0&&y<c&&z>=0&&z<l&&maze[z][x][y]!='#')
        return 1;
    else
        return 0;
}

void bfs()
{
    memset(vis,0,sizeof(vis));
    queue <node> q;
    //把起点放入队列,并且标记
    node st,ed;
    st.x = sx;
    st.y = sy;
    st.z = sz;
    st.step = 0;
    vis[st.z][st.x][st.y]=1;
    q.push(st);
    //不断循环知道队列的长度为0
    while(!q.empty())
    {
        //从队列的最前端取除元素看是否有路可通,如果没有路则返回这个点
        st = q.front();
        q.pop();
        //如果取出来是终点,则证明逃出地牢 搜索结束
        if(maze[st.z][st.x][st.y]=='E')
        {
            printf("Escaped in %d minute(s).\n",st.step);
            return;
        }
        //六个方向遍历
        for(int i = 0 ; i < 6 ;i++)
        {
            ed.z = st.z + dir[i][0];
            ed.x = st.x + dir[i][1];
            ed.y = st.y + dir[i][2];
            ed.step = st.step;
            if(check(ed.z,ed.x,ed.y)==0) //如果此方向不通就继续判断其他方向
                continue;
            ed.step++;
            if(vis[ed.z][ed.x][ed.y]==0)
            {
                vis[ed.z][ed.x][ed.y] = 1;
                q.push(ed);
            }
        }
    }
    printf("Trapped!\n");
}

int main()
{
    while(scanf("%d%d%d",&l,&r,&c)&&l&&r&&c)
    {
        for(int i = 0; i < l; i++)//输入各个点的坐标
        {
            for(int j = 0; j < r; j++)
            {
                scanf("%s",maze[i][j]);
                for(int k = 0; k < c ; k++)
                {
                    if(maze[i][j][k]=='S')
                    {
                        sz = i;
                        sx = j;
                        sy = k;
                    }

                }
            }
        }

        bfs();
    }
}


 

猜你喜欢

转载自blog.csdn.net/Xuedan_blog/article/details/80594856
今日推荐