亡命逃窜 nyoj 523

bfs  http://acm.nyist.net/JudgeOnline/problem.php?pid=523

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
int k,a,b,c,t;
struct node{
	int x,y,z;
	int step;
	node(){
		step=0;
	}
};
int p[51][51][51];
int vis[52][52][52];

int check(int x,int y,int z){
	if(x>=a||y>=b||z>=c||x<0||y<0||z<0){
		return 0;
	}
	if(p[x][y][z]==1){
		return 0;
	}
	if(vis[x][y][z]==1){
		return 0;
	}
	vis[x][y][z]=1;
	return 1;
}

int bfs(){
	vis[0][0][0]=1;
	queue<node>q;
	struct node t;
	t.x=0;t.y=0;t.z=0;t.step=0;
	q.push(t);
	while(!q.empty()){
		struct node temp=q.front();
		q.pop();
		int xx=temp.x;int yy=temp.y;int zz=temp.z;
		if(xx==a-1&&yy==b-1&&zz==c-1){
			return temp.step;
		}
		if(check(xx+1,yy,zz)){
			t.x=xx+1;t.y=yy;t.z=zz;
			t.step=temp.step+1;
			q.push(t);
		}
		if(check(xx,yy+1,zz)){
			t.x=xx;t.y=yy+1;t.z=zz;
			t.step=temp.step+1;
			q.push(t);
		}
		if(check(xx,yy,zz+1)){
			t.x=xx;t.y=yy;t.z=zz+1;
			t.step=temp.step+1;
			q.push(t);
		}
		if(check(xx-1,yy,zz)){
			t.x=xx-1;t.y=yy;t.z=zz;
			t.step=temp.step+1;
			q.push(t);
		}
		if(check(xx,yy-1,zz)){
			t.x=xx;t.y=yy-1;t.z=zz;
			t.step=temp.step+1;
			q.push(t);
		}
		if(check(xx,yy,zz-1)){
			t.x=xx;t.y=yy;t.z=zz-1;
			t.step=temp.step+1;
			q.push(t);
		}
	}
	return -1;
}

int main(){
	int temp;
	scanf("%d",&k);
	while(k--){
		scanf("%d%d%d%d",&a,&b,&c,&t);
		memset(vis,0,sizeof(vis));
    	for(int i=0;i<a;i++){
	    	for(int j=0;j<b;j++){
		    	for(int l=0;l<c;l++){
		    		scanf("%d",&p[i][j][l]);
		    	}
	    	} 
    	}
	    int flag=bfs();
	    if((flag>t)||(flag==-1)){
	    	printf("-1\n");
	    }
	    else{
	    	printf("%d\n",flag);
		}
	}
	
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值