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<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<queue>
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<L&&x>=0&&x<R&&y>=0&&y<C&&map[z][x][y]!='#')
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
void bfs()//深搜寻找可以通过的路径
{
    memset(vis,0,sizeof(vis));//将VIS初始化
    queue<node>q;//定义一个队列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<L; i++)//输入各个点的坐标
        {
            for(int j=0; j<R; j++)
            {
                scanf("%s",map[i][j]);
                for(int k=0; k<C; k++)
                {
                    if(map[i][j][k]=='S')
                    {
                        sz=i;
                        sx=j;
                        sy=k;
                    }
                }
            }
        }
        bfs();
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值