迷宫问题(进阶版)bfs算法,

作者分享了使用深度优先搜索(DFS)和广度优先搜索(BFS)解决迷宫问题的C++实现。通过比较,发现BFS在解决此类问题时通常速度更快。文章还提到了迷宫初始化的优化方法,但指出这可能导致较高的运行时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

昨天我用深度优先算法写了一道迷宫问题,见下链接

深度优先算法是实列

今天我想试一下广度优先算法看dfs与bfs算法,看哪种运算速度更快;(结果一个小错误没发现,做了好久。。。。。)

但好歹写出来了:

深度优先算法

广度优先算法

确实广度优先算法要快一些(当然还得根据不同代码及所占内存分析)

广度优先算法解题思路如下:

  1. 使用queue常用函数,应用队列的思想来处理数据

  1. 将每一个点的坐标及移动方向用结构体存储

  1. 广度优先搜索有点像辐射,从一个节点向另一个节点传递,一层一层的辐射直至目标节点被找到;

代码如下:

#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
string str;
int flag[100][100];
int map[30][50] =
{
0,1,0,1,0,1,0,1,0,0,1,0,1,1,0,0,1,0,0,1,0,1,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,0,0,0,1,0,0,0,1,0,1,0,1,0,
0,0,0,0,1,0,0,0,1,0,0,0,0,0,1,0,1,0,1,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,1,1,0,0,1,1,0,1,0,0,1,0,1,
0,1,1,1,1,0,1,1,0,1,0,0,1,0,0,0,1,0,0,0,0,0,1,1,0,1,0,0,1,0,1,1,1,0,0,0,1,1,0,0,0,0,0,0,0,1,0,0,0,0,
0,1,0,0,0,0,0,0,0,0,1,0,1,0,1,0,0,0,1,1,0,1,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,1,0,1,0,1,1,0,0,1,0,1,1,
0,0,0,1,1,1,1,1,0,0,0,0,0,0,1,0,1,0,0,0,0,1,0,0,1,0,1,0,0,0,1,0,1,0,0,0,0,0,1,0,1,1,0,0,0,0,0,0,0,0,
1,1,0,0,1,0,0,0,1,1,0,1,0,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0,1,0,0,0,0,1,1,1,0,1,0,0,0,0,0,0,1,1,0,0,1,1,
1,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,1,0,0,0,1,1,1,1,1,0,0,0,1,0,1,0,1,0,0,1,0,1,0,0,0,0,0,0,1,0,0,0,
1,0,0,0,0,0,1,0,1,0,0,1,0,1,0,0,1,0,1,0,1,1,0,0,0,0,0,0,0,1,0,0,1,0,1,0,1,0,0,0,1,0,1,1,1,0,1,0,0,0,
0,0,1,1,1,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,1,1,0,1,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,1,1,
1,0,0,0,0,0,0,1,1,0,0,1,1,1,0,1,0,1,1,1,0,1,0,0,0,1,0,0,0,1,1,0,1,1,1,0,1,0,1,0,1,1,0,1,1,1,1,0,0,0
};
int row = 30;
int col = 50;
char dir[4] = { 'D','L','R','U' };
int dirx[4] = { 1,0,0,-1 };
int diry[4] = { 0,-1,1,0 };
struct node {
    int x, y;
    string s;
};
bool check(int x, int y)
{
    if (x >= 0 && x < 30 && y >= 0 && y < 50 && !map[x][y] && !flag[x][y])
        return true;
    return false;
}
void bfs()
{
    queue<node>q;
    q.push({ 0,0,"" });
    flag[0][0] = 1;
    while (q.size())
    {
        node t = q.front();
        q.pop();
        if (t.x == (row-1) && t.y == (col-1))
        {
            cout << t.s << endl;
            return;
        }
        for (int i = 0; i < 4; i++)
        {
            int tx = t.x + dirx[i];
            int ty = t.y + diry[i];
            if (check(tx,ty))
            { 
                q.push({ tx,ty, t.s+ dir[i] });
                flag[tx][ty] = 1;
            }
        }
    }
}
int main()
{
    bfs();
    return 0;
}

其实对迷宫的初始化我也学到一种比较优雅的方法(我其实感觉初始化这个一堆0和1的迷宫感到很别扭)代码如下:

#include<iostream>
#include<queue>
#include<algorithm>
#include<time.h>
clock_t start_time = clock();
using namespace std;
string str;
int flag[100][100],map[100][100];
int row = 30;
int col = 50;
char dir[4] = { 'D','L','R','U' };
int dirx[4] = { 1,0,0,-1 };
int diry[4] = { 0,-1,1,0};
struct node {
    int x, y;
    string s;
};
bool check(int x, int y)
{
    if (x >= 0 && x < 30 && y >= 0 && y < 50 && !map[x][y] && !flag[x][y])
        return true;
    return false;
}
void bfs()
{
    queue<node>q;
    q.push({ 0,0,"" });
    flag[0][0] = 1;
    while (q.size())
    {
        node t = q.front();
        q.pop();
        if (t.x == (row-1) && t.y == (col-1))
        {
            cout << t.s << endl;
            return;
        }
        for (int i = 0; i < 4; i++)
        {
            int tx = t.x + dirx[i];
            int ty = t.y + diry[i];
            if (check(tx,ty))
            { 
                q.push({ tx,ty, t.s+ dir[i] });
                flag[tx][ty] = 1;
            }
        }
    }
}
int main()
{
   
    for (int i = 0; i < row; i++)
    {
        cin >> str;
        for (int j = 0; j < col; j++)
        {
            if (str[j] == '1')
                map[i][j] =1;
        }
    }
    bfs();
    clock_t end_time = clock();
    cout<< "Running time is: "<<static_cast<double>(end_time-start_time)/CLOCKS_PER_SEC*1000<<"ms"<<endl;
    return 0;
}

是的我认为

这一段非常

优雅❤

但是它又有一点缺陷

运算时间太长😅😅😅😅😅


好了,希望自己明天能有一个超级棒的状态学习,不虚度光阴!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值