C++ bfs再探迷宫游戏(五十五)【第二篇】

今天我们用bfs解决迷宫游戏。

1.再探迷宫游戏

前面我们已经接触过了迷宫游戏,并且学会了如何使用 DFS 来解决迷宫最短路问题。用 DFS 求解迷宫最短路有一个很大的缺点,需要枚举所有可能的路径,读入的地图一旦很大,可能的搜索方案数量会非常多,用 DFS 搜索显然效率会很低。

我们可以借助 BFS 来求解迷宫游戏。由于 BFS 是分层搜索,因此,第一次搜索到终点的时候,当前搜索的层数就是最短路径的长度。

图片

如果我们要求解起点到某个点的最短距离时,可以设置 int dis[maxn][maxn]; 记录起点到达每个点的最短距离。因为 bfs 搜索过程中第一次搜索到终点的时候是最短距离,所以可以让 dis 数组初始化为 −1−1,搜索过程中如果刚搜索到的点的 dis 等于 −1−1,表示这个点是第一次搜索到的点,需要更新 dis 数组。

程序:

#include <iostream>
#include <string>
#include <queue>
using namespace std;
int n, m;
string maze[110];
bool vis[110][110];
int dir[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
bool in(int x, int y) {
    return 0 <= x && x < n && 0 <= y && y < m;
}
struct node {
    int x, y, d;
    node (int _x, int _y, int _d) {
        x = _x;
        y = _y;
        d = _d;
    }
};
int bfs (int sx, int sy){//从起点开始搜索
    queue <node> q;
    q.push(node (sx, sy, 0));
    vis[sx][sy] = true;//开始标记
    while (!q.empty()) {
        node now = q.front();//取出状态
        q.pop();//删除状态
        if (maze[now.x][now.y] == 'T') {
            return now.d;
        }
        for (int i = 0; i < 4; i++) {
            int tx = now.x + dir[i][0];
            int ty = now.y + dir[i][1];
            //横纵变化
            if (in(tx, ty) && maze[tx][ty] != '*' && !vis[tx][ty]) {//合法性判断
                vis[tx][ty] = true;
                q.push(node(tx, ty, now.d + 1));
            }
        }
    }
    return -1; //搜索失败
}
int main() {
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        cin >> maze[i];
    }
    
    int x, y;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (maze[i][j] == 'S') {
                x = i;
                y = j;
            }
        }
    }
    cout << bfs(x, y) << endl;
    return 0;
}
  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值