#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
****************************************************************/
题目1456:胜利大逃亡
最新推荐文章于 2021-02-08 22:36:02 发布