hdu1253

#include<bits/stdc++.h>
using namespace std;
int a,b,c,T,vis[55][55][55];
int dir[6][3]={1,0,0,0,1,0,-1,0,0,0,-1,0,0,0,1,0,0,-1};
int g[55][55][55];
struct ss{
	int x,y,z,step;
}; 
int check(int x,int y,int z){
	if(x>=0&&x<a&&y>=0&&y<b&&z>=0&&z<c)
		return 1;
	return 0;
}
void  bfs(){
	queue<ss> q;
	ss cur,tem;
	cur.x=cur.y=cur.z=0;cur.step=0;
	q.push(cur);vis[cur.x][cur.y][cur.z]=1;
	while(!q.empty()){
		cur=q.front();q.pop();
		if(cur.x==a-1&&cur.y==b-1&&cur.z==(c-1)&&cur.step<=T) {
			cout<<cur.step<<endl;return ;
		}
		for(int i=0;i<6;i++){
			tem.x=cur.x+dir[i][0];
			tem.y=cur.y+dir[i][1];
			tem.z=cur.z+dir[i][2];
			if(check(tem.x,tem.y,tem.z)&&!vis[tem.x][tem.y][tem.z]){
				tem.step=cur.step+1;
				vis[tem.x][tem.y][tem.z]=1;
				q.push(tem);
			}
		}
	}
	cout<<"-1"<<endl;return;
}
int main(){
	int N;scanf("%d",&N); 
	while(N--){
		scanf("%d%d%d%d",&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++){
					scanf("%d",&g[i][j][k]);
					vis[i][j][k]=0;
					if(g[i][j][k]==1)
						vis[i][j][k]=1;
				}
		bfs();
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值