算法-BFS广度优先搜索解决迷宫问题(C++)

本文介绍了一种使用广度优先搜索(BFS)解决迷宫最短路径问题的方法。通过将起点加入队列,并逐步扩展到相邻节点,最终找到从起点到终点的最短路径。适用于初学者学习算法。

分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请轻击人工智能教程大家好!欢迎来到我的网站! 人工智能被认为是一种拯救世界、终结世界的技术。毋庸置疑,人工智能时代就要来临了,科… 继续阅读 前言https://www.captainai.net/troubleshooter

/*
 * 问题:求解迷宫问题。
 *
 * 分析:
 * 方法:BFS广度优先搜索。
 * 算法:
 * 1、将起点入队。
 * 2、队首结点可扩展的点入队;如果没有可扩展的点,将队首结点出队。
 * 3、重复第2步,直到到达目标位置或者队列为空。
 * 注:BFS搜索到的结果一定是最短的。BFS运用到了队列。
 *
 * Maze_BFS.cpp - by LiveEveryDay
 */

#include <queue>

using namespace std;

int startX, startY, endX, endY, row, col;
int a[100][100]; // 1表示空地,2表示障碍物。
int v[100][100]; // 0表示未访问,1表示已访问。

struct point {
    int x;
    int y;
    int step;
};

queue<point> q; // 申请队列
int dx[4] = {0, 1, 0, -1}; // 四个方向(右、下、左、上)在X轴的偏移量。
int dy[4] = {1, 0, -1, 0}; // 四个方向(右、下、左、上)在Y轴的偏移量。

void bfs() {
    point start;
    start.x = startX;
    start.y = startY;
    start.step = 0;
    q.push(start);
    v[startX][startY] = 1;
    int flag = 0;
    while (!q.empty()) {
        int x = q.front().x;
        int y = q.front().y;
        if (x == endX && y == endY) {
            flag = 1;
            printf("The min step is: %d", q.front().step);
            break;
        }

        // 顺时针试探
        for (int i = 0; i <= 3; i++) {
            int tx = x + dx[i];
            int ty = y + dy[i];
            if (a[tx][ty] == 1 && v[tx][ty] == 0) {

                // 入队
                point t;
                t.x = tx;
                t.y = ty;
                t.step = q.front().step + 1;
                q.push(t);
                v[tx][ty] = 1;
            }
        }
        q.pop(); // 拓展完成,将队首元素出队。
    }
    if (flag == 0) {
        printf("No solution!");
    }
}

int main() {
    printf("Input row and col:\n");
    scanf("%d%d", &row, &col);
    printf("Input the map:\n");
    for (int x = 1; x <= row; x++) {
        for (int y = 1; y <= col; y++) {
            scanf("%d", &a[x][y]); // 1表示空地,2表示障碍物。
        }
    }
    printf("Input start position:\n");
    scanf("%d%d", &startX, &startY);
    printf("Input end position:\n");
    scanf("%d%d", &endX, &endY);

    bfs();

    return 0;
}

// ------ Input ------
/*
Input row and col:
5 4
Input the map:
1 1 2 1
1 1 1 1
1 1 2 1
1 2 1 1
1 1 1 2
Input start position:
1 1
Input end position:
4 3
*/

// ------ Output ------
/*
The min step is: 7
*/
好的,我可以为您讲解C++使用广度优先搜索算法解决迷宫问题广度优先搜索算法是一种用于遍历或搜索树或图的算法,它从起点开始,逐层遍历每个可能的路径,直到找到目标节点或无法继续搜索为止。 在解决迷宫问题时,我们可以将迷宫看作一个图,其中每个格子都是一个节点,相邻的格子之间有一条边。我们可以使用广度优先搜索算法从起点开始遍历图,直到找到终点为止。在搜索过程中,我们需要记录每个节点的深度,并且每次遍历到一个节点时,都需要判断它是否是终点,如果是终点则搜索结束,否则继续向下搜索。 以下是C++代码示例: ```c++ #include <iostream> #include <queue> //使用STL库中的队列 #define ROW 5 #define COL 5 using namespace std; struct Node{ int x, y, depth; }; int maze[ROW][COL] = { {0, 0, 0, 0, 0}, {0, 1, 1, 0, 0}, {0, 1, 1, 0, 0}, {0, 0, 0, 1, 0}, {0, 0, 0, 1, 0} }; int visited[ROW][COL] = {0}; //记录节点是否被访问过 int dx[4] = {0, 0, 1, -1}; //定义四个方向的移动 int dy[4] = {1, -1, 0, 0}; void bfs(int x, int y){ queue<Node> q; //定义队列 Node start = {x, y, 0}; //起点节点 q.push(start); //将起点节点入队 visited[x][y] = 1; //标记起点节点已访问 while(!q.empty()){ //队列不为空时循环 Node cur = q.front(); //取出队首元素 q.pop(); //将队首元素出队 if(cur.x == ROW-1 && cur.y == COL-1){ //到达终点 cout << "Find the way! The shortest path length is " << cur.depth << endl; return; } for(int i=0; i<4; ++i){ //向四个方向进行搜索 int nx = cur.x + dx[i]; int ny = cur.y + dy[i]; if(nx<0 || nx>=ROW || ny<0 || ny>=COL) continue; //越界跳过 if(visited[nx][ny] || maze[nx][ny]) continue; //已访问或者是墙跳过 visited[nx][ny] = 1; //标记为已访问 Node next = {nx, ny, cur.depth+1}; //将下一个节点入队 q.push(next); } } cout << "Can't find the way!" << endl; //无法到达终点 } int main(){ bfs(0, 0); //从起点开始搜索 return 0; } ``` 在上述代码中,我们使用广度优先搜索算法从起点开始遍历迷宫,并且使用visited数组记录节点是否被访问过。在搜索过程中,我们向四个方向进行搜索,直到找到终点或者无法继续搜索为止。如果找到了终点,则输出"Find the way! The shortest path length is xxx",其中xxx表示最短路径的长度,搜索结束。如果无法到达终点,则输出"Can't find the way!"。 希望这个例子可以帮助您理解C++使用广度优先搜索算法解决迷宫问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值