NYOJ 3D dungeon(bfs深搜)

3D dungeon

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 2
描述
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? 
输入
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.
输出
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!
样例输入
3 4 5
S....
.###.
.##..
###.#

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

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

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

0 0 0
样例输出
Escaped in 11 minute(s).

Trapped!

题意:

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

思路:

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

代码:

#include
#include
#include
#include
#include
#include
using namespace std;
char map[50][50][50];//坐标
int L,R,C,sz,sx,sy;
int vis[50][50][50];//标记该点是否访问
int dir[6][3]= {{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};//前后左右上下六个方向坐标
{
    int x,y,z,t;
};
int check(int z,int x,int y)//如果可以通过返回1,不可以通过返回0
{
    if(z>=0&&z=0&&x=0&&yq;//定义一个队列q存储符合条件的坐标
    node st,ed;
    st.z=sz;
    st.x=sx;
    st.y=sy;
    st.t=0;
    vis[st.z][st.x][st.y]=1;
    q.push(st);//将出发点入队
    while(!q.empty())//如果队列q为空就证明没找到可以通过的路径
    {
        st=q.front();//首先对队首元素进行深搜看六个方向有没有可以通路的
        q.pop();
        if(map[st.z][st.x][st.y]=='E')//如果队首元素为E证明逃出地牢
        {
            printf("Escaped in %d minute(s).\n",st.t);
            return;
        }
        for(int i=0; i<6; i++)//对六个方向进行遍历
        {
            ed=st;
            ed.z+=dir[i][0];
            ed.x+=dir[i][1];
            ed.y+=dir[i][2];
            if(check(ed.z,ed.x,ed.y)==0)//如果此方向不能通过就继续对其他方向进行搜索
            {
                continue;
            }
            ed.t++;//可以通过时间加一
            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


你可能感兴趣的:(bfs)