BFS——1253 胜利大逃亡

1253 胜利大逃亡

请添加图片描述

文章目录


题意:

在这里插入图片描述

思路:

将BFS原来二维的部分改成三维,其余部分均不变即可。
注:

用cin读入要关流,否则会超时

读入优化代码:

std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);

AC代码:
在这里插入图片描述

#include<bits/stdc++.h>

typedef long long ll;

const int N = 60,M = 2e4+10,INF = 0x3f3f3f3f,mod = 1e9+7;
struct Node{
    int x,y,z,step;
}que[N*N*N];

int a,b,c,t;
bool g[N][N][N],book[N][N][N];
int ne[6][3] = {{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};

bool OK(int x,int y,int z)
{
    if(x<1||y<1||z<1||x>a||y>b||z>c||book[x][y][z]||g[x][y][z])return false;
    return true;
}

void bfs()
{
    int tail = -1,head = 0;
    que[++tail] = {1,1,1};
    book[1][1][1] = true;
    while(tail >= head)
    {
        auto temp = que[head++];
        if(temp.x == a && temp.y == b && temp.z == c)
        {
            std::cout<<temp.step<<'\n';
            return;
        }
        if(temp.step>t)continue;
        for(int i = 0 ; i < 6 ; i++)
        {
            int tx = temp.x + ne[i][0];
            int ty = temp.y + ne[i][1];
            int tz = temp.z + ne[i][2];
            if(!OK(tx,ty,tz))continue;
            book[tx][ty][tz] = true;
            que[++tail] = {tx,ty,tz,temp.step+1};
        }
    }
    std::cout<<-1<<'\n';
}

int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    int T;
    std::cin>>T;
    while(T--)
    {
        std::cin>>a>>b>>c>>t;
        for(int i = 1 ; i <= a ; i++)
            for(int j = 1 ; j <= b ; j++)
                for(int k = 1 ; k <= c ; k++)std::cin>>g[i][j][k],book[i][j][k] = false;
        bfs();
    }
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值