【三维bfs】HDU1253——胜利大逃亡

来源:点击打开链接

三维BFS,比起二维BFS来说多了一维,但是也没什么难度。

第一次卡时间过,在大神的指导下明白了,还可以进行优化。

1、搜索方向变成了6个。

2、需要考虑到出口处为墙的情况。

3、如果有这个会大大提高效率:if(a+b+c-3>tarstep) {printf("-1\n");continue;}(起点与终点间最短路径大于时间限度)

CODE:

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

int dir[6][3]= {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
int a,b,c,tarstep;
int mat[60][60][60];
int visited[60][60][60];


class node
{
public:
    int x;
    int y;
    int z;
    int step;
};

node s1,e1;

int judge(int x,int y,int z)
{
    if(x>=0 && x<a && y>=0 && y<b && z>=0 && z<c && mat[x][y][z]==0 && visited[x][y][z]==0)
        return 1;
    else
        return 0;
}

int bfs(node st,node ed)
{

    if(mat[ed.x][ed.y][ed.z]==1)//特判1
        return -1;
    if(a+b+c-3>tarstep) //特判2
    {
        return -1;
    }
    memset(visited,0,sizeof(visited));
    queue<node> q;
    node first,next;
    first.x=st.x;
    first.y=st.y;
    first.z=st.z;
    first.step=0;

    visited[first.x][first.y][first.z]=1;
    q.push(first);
    while(!q.empty())
    {
        first=q.front();
        q.pop();
        for(int i=0; i<6; i++)
        {
            next.x=first.x+dir[i][0];
            next.y=first.y+dir[i][1];
            next.z=first.z+dir[i][2];
            next.step=first.step+1;

            if(next.x==ed.x && next.y==ed.y && next.z==ed.z && next.step<=tarstep)
                    return next.step;

            if(judge(next.x,next.y,next.z))
            {
                    visited[next.x][next.y][next.z]=1;
                    q.push(next);

            }
        }
    }
    return -1;
}

int main()
{
    int testcase;
    scanf("%d",&testcase);
    while(testcase--)
    {

        scanf("%d%d%d%d",&a,&b,&c,&tarstep);

        for(int i=0;i<a;i++)
        {
            for(int j=0;j<b;j++)
            {
                for(int k=0;k<c;k++)
                {
                    scanf("%d",&mat[i][j][k]);
                }
            }
        }
        s1.x=0;
        s1.y=0;
        s1.z=0;
        s1.step=0;
        e1.x=a-1;
        e1.y=b-1;
        e1.z=c-1;
        e1.step=0;
        int res=bfs(s1,e1);
        printf("%d\n",res);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个基于C++的BFS求二维迷宫中最短路径的示例代码: ```cpp #include <iostream> #include <queue> #include <cstring> using namespace std; const int MAXN = 1005; // 最大节点数 const int INF = 0x3f3f3f3f; // 无穷大 int n, m; // n表示迷宫的行数,m表示迷宫的列数 int sx, sy, ex, ey; // 起点和终点的坐标 int maze[MAXN][MAXN]; // 存储迷宫的地图 int dis[MAXN][MAXN]; // 存储起点到每个节点的距离 bool vis[MAXN][MAXN]; // 记录每个节点是否被访问过 struct node { int x, y; // 节点坐标 }; void bfs() { memset(dis, INF, sizeof(dis)); // 初始化距离为无穷大 memset(vis, false, sizeof(vis)); // 初始化所有节点都未被访问过 dis[sx][sy] = 0; // 起点到自己的距离为0 vis[sx][sy] = true; // 起点已经访问过了 queue<node> q; // 定义一个队列,用于广度优先搜索 q.push({sx, sy}); // 将起点加入队列 while (!q.empty()) { node u = q.front(); q.pop(); // 取出队首节点 int dx[4] = {0, 0, 1, -1}; // 四个方向 int dy[4] = {1, -1, 0, 0}; for (int i = 0; i < 4; ++i) { int x = u.x + dx[i]; int y = u.y + dy[i]; if (x >= 1 && x <= n && y >= 1 && y <= m && maze[x][y] != 1 && !vis[x][y]) { // 如果这个点可行且未被访问过 dis[x][y] = dis[u.x][u.y] + 1; // 更新距离 vis[x][y] = true; // 标记为已访问 q.push({x, y}); // 将其加入队列 } } } } int main() { cin >> n >> m; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { cin >> maze[i][j]; } } cin >> sx >> sy >> ex >> ey; bfs(); cout << "起点到终点的最短距离为:" << dis[ex][ey] << endl; return 0; } ``` 这段代码使用二维数组存储迷宫地图和节点距离,时间复杂度为O(nm),其中n表示迷宫的行数,m表示迷宫的列数。需要注意的是,在实际使用中,可能需要根据具体情况调整代码。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值