bfs 3D迷宫

#include<cstdio>
#include<cstring>
#include<queue>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=35;
bool vst[maxn][maxn][maxn]; // 访问标记
char map[maxn][maxn][maxn];
int z,x,y,ac;//题目输入
int dir[6][3]={
    {1,0,0},{-1,0,0},{0,1,0},
    {0,-1,0},{0,0,1},{0,0,-1}
}; // 方向向量

struct State // BFS 队列中的状态数据结构
{
    int x,y,z; // 坐标位置
    char v;
    int Step_Counter; // 搜索步数统计器
};
bool CheckState(State s) // 约束条件检验
{
    if((!vst[s.x][s.y][s.z])&&s.x<=x&&s.x>=1&&s.y<=y&&s.y>=1&&s.z<=z&&s.z>=1&&map[s.x][s.y][s.z]!='#') // 满足条件
        return 1;
    else{ // 约束条件冲突
        vst[s.x][s.y][s.z]=1;
        return 0;

    }
}

void bfs(State st)
{
    queue <State> q; // BFS 队列
    State now,next; // 定义2 个状态,当前和下一个
    st.Step_Counter=0; // 计数器清零
    q.push(st); // 入队
    vst[st.x][st.y][st.z]=1; // 访问标记
    while(!q.empty())
    {
        now=q.front(); // 取队首元素进行扩展
        if(map[now.x][now.y][now.z]=='E') // 出现目标态,此时为Step_Counter 的最小值,可以退出即可
        {
            ac=now.Step_Counter; // 做相关处理
            //cout<<11111111111111<<endl;
            return;
        }
        //cout<<now.x<<now.y<<now.z<<endl;
        for(int i=0;i<6;i++)
        {
            next.x=now.x+dir[i][0]; // 按照规则生成下一个状态
            next.y=now.y+dir[i][1];
            next.z=now.z+dir[i][2];
            //cout<<i<<'^'<<"x=x"<<dir[i][0]<<" y=y"<<dir[i][1]<<" z=z"<<dir[i][2]<<endl;
            next.Step_Counter=now.Step_Counter+1; // 计数器加1
            if(CheckState(next)) // 如果状态满足约束条件则入队
            {
                q.push(next);
                //cout<<next.x<<next.y<<next.z<<endl;
                vst[next.x][next.y][next.z]=1; //访问标记
            }
        }
        q.pop(); // 队首元素出队
    }
    return;
}
int main()
{

    while(cin>>z>>x>>y)
    {
        State st;
        if(x==0&&y==0&&z==0)break;
        ac=-1;
        for(int zz=1;zz<=z;zz++)
        {
            for(int xx=1;xx<=x;xx++)
            {
                for(int yy=1;yy<=y;yy++)
                {
                    cin>>map[xx][yy][zz];
                    //cout<<map[xx][yy][zz];
                    vst[xx][yy][zz]=0;
                    if(map[xx][yy][zz]=='S'){st.x=xx,st.y=yy,st.z=zz,st.v=map[1][1][1];}

                }
                //cout<<endl;

            }
            //cout<<endl;
        }//读地图完成


        bfs(st);
        if(ac!=-1)cout<<"Escaped in "<<ac<<" minute(s)."<<endl;
        else cout<<"Trapped!"<<endl;




    }
}
/*
POJ - 2251
 Description - 题目描述
   你被困在一个3D地牢中且继续寻找最短路径逃生!地牢由立方体单位构成,立方体单位中有的会充满岩石。向上下前后左右移动一个单位需要一分钟。你不能向对角线的四个方向移动且迷宫四周环绕着许多岩石。

   是否可以逃出地牢?如果可以,则需要多少时间?
 Input - 输入
   输入的第一行包含一个数,表示地牢的数量。
   每个地牢的描述,其第一行包含三个数L,R和C(均小于等于30)。
   L表示地牢的层数;R和C分别表示每层地牢的行与列的大小。

   随后输入地牢的层数L,每层中包含R行,每行中包含C个字符。
   每个字符表示地牢的一个单元。'#'表示岩石单元,'.'表示空白单元。你的起始位置在点'S',出口为'E'。
   每层地牢的输入后都有一个空行。当L,R和C均为0时,输入结束。
 Output - 输出
   每个迷宫对应一行输出。
   如果可以逃生,则输出如下
 Escaped in x minute(s).
   x为最短脱离时间。

   如果无法逃生,则输出如下
 Trapped!
 Sample Input - 输入样例

 3 4 5
 S....
 .###.
 .##..
 ###.#

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

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

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

 0 0 0

 Sample Output - 输出样例

 Escaped in 11 minute(s).
 Trapped!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值