hdu 1253 胜利大逃亡 BFS

题目分析:{1,0,0}写成{0,1,0},,,囧呀 找了半天错误,,,,,

#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;

int maze[55][55][55],k,a,b,c,t,flag,ans;
int move[6][3]={{0,0,1},{0,1,0},{1,0,0},
{0, 0,-1},{0,-1,0},{-1,0,0}};//下 右 前 上 左 后{0,1,0}!!!!!!!!!!!!!
struct node{
	int x;
	int y;
	int z;
	int t;
}begin,hash[55][55][55];
int IN(int x,int y,int z)
{
   if(x<=0||x>a ||y<=0||y>b ||z<=0||z>c)
	   return 0;
   else
	   return 1;
}
void BFS()
{
	int i;
	node cur,temp;
    queue<node>q;
	begin.x=1,begin.y=1,begin.z=1;
	begin.t=0;
	maze[begin.x][begin.y][begin.z]=1;//起点忘标记了
	q.push(begin);
	while(!q.empty())
	{
		cur=q.front();
		q.pop();
		/*if((cur.x+cur.y+cur.z-a-b-c)-(t-cur.t)>0)//错过
			continue; //return; 剪枝*/
		for(i=0;i<6;i++)
		{
			temp.x=cur.x+move[i][0];
			temp.y=cur.y+move[i][1];
			temp.z=cur.z+move[i][2];
			temp.t=cur.t+1;
			if(temp.x==a&&temp.y==b&&temp.z==c)
			{
				flag=1;
				ans=temp.t;
				return;
			}
			if(IN(temp.x,temp.y,temp.z) && maze[temp.x][temp.y][temp.z]==0)
			{
				maze[temp.x][temp.y][temp.z]=1;//走过的节点 都被标记 免得重走
				q.push(temp);
			}
		}
	}
}
int main()
{
	int i,j,m;
	scanf("%d",&k);
	while(k--)
	{
		scanf("%d %d %d %d",&a,&b,&c,&t);
		for(i=1;i<=a;i++)
			for(j=1;j<=b;j++)
				for(m=1;m<=c;m++)
					scanf("%d",&maze[i][j][m]);//搞成  abm了
        /***for(i=1;i<=a;i++)
		{
			for(j=1;j<=b;j++)
			{
				for(m=1;m<=c;m++)
					printf("%d ",maze[i][j][m]);
				printf("\n");
			}
			printf("\n");
		}
		cout<<endl<<endl;*/
        flag=0,ans=0;
		if(maze[a][b][c]==1||a+b+c-t>0)
			printf("-1\n");
		else
		{
	           BFS();
		   if(flag==0||ans>t)
		    	printf("-1\n");
		   else
		    	printf("%d\n",ans);
		}
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值