现在的位置: 首页 > 综合 > 正文

hdu 1253 胜利大逃亡

2018年12月28日 ⁄ 综合 ⁄ 共 897字 ⁄ 字号 评论关闭

广搜

 

 

 

#include<stdio.h>
#include<queue>
using namespace std;
#define N 55
struct node
{
    int x,y,z,step;
};
int n,m,t,h,dir[6][3]={-1,0,0, 1,0,0, 0,-1,0, 0,1,0, 0,0,-1, 0,0,1}; 
char map[N][N][N];
int judge(int x,int y,int z)
{
    if(x>=0 && x<n && y>=0&&y<m &&z>=0&&z<h &&map[x][y][z]==0)
        return 1;
    return 0;
}

int bfs()
{
	queue<node>q;
	node cur,next;
	int x,y,z,k;
    cur.x=cur.y=cur.z=cur.step=0;
	q.push(cur);
	map[0][0][0]=1;
	while(!q.empty())
	{
		cur=q.front();
		q.pop();
		if(cur.step>=t)
			return -1;
		next.step=cur.step+1;
		for(k=0;k<6;k++)
		{
			next.x=x=cur.x+dir[k][0];
			next.y=y=cur.y+dir[k][1];
			next.z=z=cur.z+dir[k][2];
			if(judge(x,y,z))
			{
				if(x==n-1&&y==m-1&&z==h-1)
					return next.step;
				map[x][y][z]=1;
				q.push(next);
			}
		}
	}
	return -1;
}
int main()
{
    int i,j,ans,flag,l,T;
    scanf("%d",&T);
	
    while(T--)
    {
        scanf("%d%d%d%d",&n,&m,&h,&t);
		getchar();flag=1;
        for(i=0;i<n;i++)
			for(j=0;j<m;j++)
				for(l=0;l<h;l++)
					scanf("%d",&map[i][j][l]);
				if(map[n-1][m-1][h-1]==1)
					ans=-1;
				else
					ans=bfs();
				printf("%d\n",ans);
	}
    return 0;
}

 

抱歉!评论已关闭.