hdu_1253 胜利大逃亡(BFS)

http://acm.hdu.edu.cn/showproblem.php?pid=1253

题意:

给你个三维空间,问在规定的时间内能否从起点走到终点。
思路:

很水的BFS啊啊啊,我挂在起始位置。。。起点不用考虑啊啊啊。。。 各种蛋疼有木有。。。 

我的代码:

/*
program:hdu_1253
author:BlackAndWhite 
*/
#include<stdio.h>
int a[51][51][51],ni,nj,nz,K,T;
int i,j,z,dir[6][3]={1,0,0, -1,0,0, 0,1,0, 0,-1,0,0,0,1, 0,0,-1};
struct node
{
    int x,y,z;
}que[1000001];
int bfs()
{
    int push=1,pop=0;
    struct node t,q;
    t.x=0;t.y=0;t.z=0;
    if(a[nz-1][ni-1][nj-1]) return 0;//起点不用考虑啊,我勒个XX 
    a[t.z][t.x][t.y]=1;que[0]=t;
    while(pop<push)
    {
        t=que[pop++];
        if(a[t.z][t.x][t.y]>T+1){return 0;}
        if(t.z==nz-1&&t.x==ni-1&&t.y==nj-1) return 1;
        for(i=0;i<6;i++)
            if(t.z+dir[i][0]<nz&&t.z+dir[i][0]>=0
            &&t.x+dir[i][1]<ni&&t.x+dir[i][1]>=0
            &&t.y+dir[i][2]<nj&&t.y+dir[i][2]>=0
            &&!a[t.z+dir[i][0]][t.x+dir[i][1]][t.y+dir[i][2]])
            {
                a[t.z+dir[i][0]][t.x+dir[i][1]][t.y+dir[i][2]]=a[t.z][t.x][t.y]+1;
                q.z=t.z+dir[i][0];
                q.x=t.x+dir[i][1];
                q.y=t.y+dir[i][2];
                que[push++]=q;
            }
    }
    return 0;
}
int main()
{
    scanf("%d",&K);
    while(K--)
    {
        scanf("%d%d%d%d",&nz,&ni,&nj,&T);
        for(z=0;z<nz;z++)
            for(i=0;i<ni;i++)
                for(j=0;j<nj;j++)
                    scanf("%d",&a[z][i][j]);
        if(bfs()) printf("%d\n",a[nz-1][ni-1][nj-1]-1);
        else printf("-1\n");
    }
    return 0;
}
/*
我勒个去,一水题淹死我了 
12

3 3 4 10
0 1 1 1
0 0 1 1
0 1 1 1

1 1 1 1
1 0 0 1
0 1 1 1

0 0 0 0
0 1 1 0
0 1 1 0


*/


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值