hdu~1253(bfs)

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

简单bfs,就是多了上下两个方向,用bfs做的话,好像一旦到了终点,这条路就是最短的。

注意seat【】【】【】中第一维存的是Z坐标,

不过由于此题起点为0 0 0,终点为a-1,b-1,c-1(我在题目中改成1,1,1到a,b,c)。所以这个没影响。

<span style="font-size:18px;">#include <stdio.h>
#include <queue>
#define MAX 55

int seat[MAX][MAX][MAX];//存图
int a,b,c,t,ans;
int dir[10][3]={0,0,0,1,0,0,0,1,0,-1,0,0,0,-1,0,0,0,1,0,0,-1};//三维的,多了两个方向

typedef struct ac
{
    int x,y,z,time;
}node;

using namespace std;

void bfs(int x,int y,int z,int time)
{
    queue <node > q;
    node next,temp;
    next.x=x;
    next.y=y;
    next.z=z;
    next.time=time;
    seat[x][y][z]=1;//走过记为1,(这里的墙也是用1表示的)<span style="white-space:pre">		</span>
    q.push(next);
    while(!q.empty())
    {
        next=q.front();
        q.pop();
        if(next.x==a && next.y==b && next.z==c)//到终点返回
        {
            ans=next.time;
            return ;
        }
        for(int i=1;i<=6;i++)
        {
            temp.x=next.x+dir[i][0];
            temp.y=next.y+dir[i][1];
            temp.z=next.z+dir[i][2];
            temp.time=next.time+1;
            if(temp.x<1 || temp.x>a || temp.y<1 || temp.y>b || temp.z<1 || temp.z>c)
                    continue;
            if(seat[temp.x][temp.y][temp.z])
                continue;

            seat[temp.x][temp.y][temp.z]=1;
            q.push(temp);
        }
    }
}
int main()
{
    int i,j,l,T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d %d %d %d",&a,&b,&c,&t);
        for(i=1;i<=a;i++)
            for(j=1;j<=b;j++)
                for(l=1;l<=c;l++)
                    scanf("%d",&seat[i][j][l]);
        ans=1005;
        bfs(1,1,1,0);
        if(ans>=t)  //未在规定时间内到达。
            printf("-1\n");
        else
            printf("%d\n",ans);
    }
    return 0;
}
</span>



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值