题目1456:胜利大逃亡

#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <cstring>
#include <string>
#include <string.h>
#include <stdio.h>
#include <cmath>
#include <map>
#include <functional>
#include <set>
using namespace std;
//1456
int k,a,b,c,t;
struct N{
    int x,y,z,t;
};
queue<N>q;        //队列保存状态
int maze[51][51][51];
bool mk[51][51][51];
int nt[][3]={   {1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}  };  //往6个方向转移的状态
int bfs(int a,int b,int c){
    while(!q.empty()){
        N now=q.front();
        q.pop();
        for(int i=0;i<6;i++){
            int nx=now.x+nt[i][0];
            int ny=now.y+nt[i][1];
            int nz=now.z+nt[i][2];
            if( nx<0 || nx>=a || ny<0 || ny>=b || nz<0 || nz>=c ) continue;
            if(maze[nx][ny][nz]==1) continue;
            if(mk[nx][ny][nz]==true)    continue;
            N tmp;
            tmp.x=nx;tmp.y=ny;tmp.z=nz;tmp.t=now.t+1;
            q.push(tmp);
            mk[nx][ny][nz]=true;
            if(nx==a-1&&ny==b-1&&nz==c-1)       return tmp.t;
        }
    }
    return -1;
}
 
int main(){
    //freopen("input.txt","r",stdin);
    while(cin>>k){
        while(k--){
            cin>>a>>b>>c>>t;
            for(int i=0;i<a;i++){
                for(int j=0;j<b;j++){
                    for(int k=0;k<c;k++){
                        cin>>maze[i][j][k];
                        mk[i][j][k]=false;
                    }
                }
            }
            while(!q.empty())   q.pop();
            N tmp;
            tmp.x=tmp.y=tmp.z=tmp.t=0;
            mk[0][0][0]=true;
            q.push(tmp);
            int res=bfs(a,b,c);
            if(res<=t)   cout<<res<<endl;
            else        cout<<-1<<endl;
        }
    }
    return 0;
}
 
/**************************************************************
    Problem: 1456
    User: cust123
    Language: C++
    Result: Accepted
    Time:30 ms
    Memory:2168 kb
****************************************************************/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值