Dungeon Master bfs

题目链接

#include <iostream>
#include <string>
#include <cstring>
#include <queue>
using namespace std;
struct node//用结构体构造
{
    int x,y,z;
    int count;
};
int l,r,c,i,j,k;
char qipan[35][35][35];
int tansuo[35][35][35];
int d[6][3] = {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};//6个方向
int x1,y1,z1;
void bfs()
{
    queue<node>Q;//创建并初始化队列
    node t1;
    t1.x=x1,t1.y=y1,t1.z=z1,t1.count=0;
    tansuo[x1][y1][z1]=1;
    Q.push(t1);
    while(!Q.empty())
    {
        node t2;
        t2=Q.front();
        Q.pop();
        int xx=t2.x;
        int yy=t2.y;
        int zz=t2.z;
        int count=t2.count;
        if(qipan[xx][yy][zz]=='E')//成功到达出口,程序结束
        {
            cout<<"Escaped in "<<count<<" minute(s)."<<endl;
            return ;

        }
        for(i=0; i<6; i++)
        {
            node t3;
            int xi=t3.x=xx+d[i][0];
            int yi=t3.y=yy+d[i][1];
            int zi=t3.z=zz+d[i][2];
            if(xi<0||xi>l-1||yi<0||yi>r-1||zi<0||zi>c-1)
                continue;//越界,跳过
            if(qipan[xi][yi][zi] != '#' && !tansuo[xi][yi][zi])//进行搜索
            {
                tansuo[xi][yi][zi]=1;//标记
                t3.count=count+1;
                Q.push(t3);//搜索完出队列
            }


        }
    }
    cout<<"Trapped!"<<endl;

}
int main()
{
    while(cin>>l>>r>>c)
    {
        if(l==0&&r==0&&c==0)
            break;
        for(i=0; i<l; i++)
        {
            for(j=0; j<r; j++)
            {
                for(k=0; k<c; k++)
                {
                    cin>>qipan[i][j][k];
                    if(qipan[i][j][k]=='S')//标记起点
                    {
                        x1=i,y1=j,z1=k;
                    }
                }
            }

        }
        memset(tansuo,0,sizeof(tansuo));
        bfs();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值