算法学习之BFS

关于BFS我的理解是根据离我们当前这个点的权重来移动,这里权重也可以理解为离这个点的距离,

从起点开始,往前走一步,记录下所有第一步能走到的点开始,然后从所有第一部能走到的点开始向前走第二步,重复下去,一直到终点,输出步数即可,

实现方式:广度优先搜索

这是一个例题:

 

下面是用队列写的方法

我之前疑惑的点是while(!q.empty())不知道什么时候停止,后来想了想,觉得如果广搜到最后一个位置的话,怎么判断,所以在代码中加了一个判断是否到达目标点的判断。

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

typedef pair<int, int> PII; // 定义坐标对类型
const int N = 110;          // 地图最大尺寸

int g[N][N]; // 存储地图,0表示可通行,1表示障碍
int f[N][N]; // 存储每个点到起点的最短距离
int n, m;    // 地图的行数和列数

// 广度优先搜索函数,起点坐标为(a, b)
void bfs(int a, int b) {
    queue<PII> q;
    q.push({ a, b });
    f[a][b] = 0; // 起点距离为0

    while (!q.empty()) {
        PII current = q.front();
        q.pop();

        // 新增:判断当前节点是否是终点
        if (current.first == n && current.second == m) {
            cout << f[current.first][current.second];
            return; // 找到终点,直接结束函数
        }

        // 方向增量:上、右、下、左
        int dx[4] = { -1, 0, 1, 0 }, dy[4] = { 0, 1, 0, -1 };
        for (int i = 0; i < 4; i++) {
            int x = current.first + dx[i];
            int y = current.second + dy[i];

            // 检查坐标合法性(注意地图是 1-based)
            if (x >= 1 && x <= n && y >= 1 && y <= m && g[x][y] == 0) {
                g[x][y] = 1; // 标记为已访问
                f[x][y] = f[current.first][current.second] + 1;
                q.push({ x, y });
            }
        }
    }

    // 队列清空仍未找到终点,说明无解
    cout << "No path!";
}
int main() {
    memset(g, 1, sizeof(g)); // 初始化地图周围为障碍(每个字节设为1,int实际值为0x01010101)
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            cin >> g[i][j]; // 读取地图数据(1-based)

    bfs(1, 1); // 从起点(1,1)开始BFS
    return 0;
}

下面这个做法是模拟队列

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

typedef pair<int, int> PII;
const int N = 110;

int g[N][N];    // 地图:0可通行,1障碍
int d[N][N];    // 到起点的距离,-1表示未访问
int n, m;       // 实际行数n,列数m
PII q[N * N];   // 数组模拟队列

int bfs() {
    int hh = 0, tt = 0;
    q[0] = {0, 0};          // 起点(0,0)入队
    memset(d, -1, sizeof(d));
    d[0][0] = 0;            // 起点距离为0
    
    // 方向:上、下、左、右
    int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
    
    while (hh <= tt) {
        auto t = q[hh++];   // 取出队头
        
        
        if (t.first == n-1 && t.second == m-1) {
            return d[t.first][t.second];
        }
        
        // 遍历四个方向
        for (int i = 0; i < 4; i++) {
            int x = t.first + dx[i], y = t.second + dy[i];
            
            // 合法性检查:不越界、可通行、未被访问
            if (x >= 0 && x < n && y >= 0 && y < m && g[x][y] == 0 && d[x][y] == -1) {
                d[x][y] = d[t.first][t.second] + 1;  // 更新距离
                q[++tt] = {x, y};                    // 新坐标入队
            }
        }
    }
    return -1;  // 队列清空仍未找到终点,返回-1
}

int main() {
    cin >> n >> m;
    
    
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)  // 正确遍历列数m
            cin >> g[i][j];
    
    cout << bfs() << endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值