hdu1240广搜

一个三维数组要你求 给定的开始点和终点 最小的 步数 所以用 广搜;
第一建立一个三维图;从给定的开始点 往六个方向进行广搜;如果满足条件就跳出;
最好不要用cin输入

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
char map[10][10][10];
int n, st1, st2, st3, en1, en2, en3, flag;
int 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;
    int y;
    int z;
    int step;
};

void bfs(int st1, int st2, int st3)
{
    queue<node>q;
    node temp, type;
    temp.x = st1;//初始化;
    temp.y = st2;
    temp.z = st3;
    temp.step = 0;
    q.push(temp);
    while(!q.empty())
    {
        temp = q.front();
        q.pop();
        if(temp.x == en1 && temp.y == en2 && temp.z == en3)//判断是否符合条件;
            {
                flag =  temp.step;
                return ;
            }
        for(int i = 0; i < 6; i++)//将六个方向的元素放到队列中;
        {
            int fx = temp.x + dir[i][0];
            int fy = temp.y + dir[i][1];
            int fz = temp.z + dir[i][2];
            if(fx < 0 || fx >= n || fy < 0 || fy >= n || fz < 0 || fz >= n || map[fx][fy][fz] == 'X')
            {
                continue;
            }
                type.x = fx;
                type.y = fy;
                type.z = fz;
                type.step = temp.step + 1;
                map[fx][fy][fz] = 'X';//标记防止在被使用;
                q.push(type);
        }
    }
    return ;
}

int main()
{
    char str[10];
    while(scanf("%s %d", str, &n)==2)//如果没有==2 则会超时;注意啊!!!
    {
        for(int k=0;k<n;k++)//建图;
        for(int i=0;i<n;i++)
        {
            getchar();
            for(int j=0;j<n;j++)
            {
                scanf("%c",&map[i][j][k]);
            }
        }
        scanf("%d %d %d",&st1,&st2,&st3);
        scanf("%d %d %d",&en1,&en2,&en3);
        scanf("%s",str);
        map[st1][st2][st3] = 'X';//标记 防止在被使用;
        flag = -1;
        bfs(st1, st2, st3);
        if(flag == -1)
            printf("NO ROUTE\n");
        else
            printf("%d %d\n", n, flag);
    }
}


 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值