返回首页 C++ 模板

BFS搜索模板

广度优先搜索——逐层扩展,天然求最短路径

BFS 广度优先 最短路径 队列 GESP6
#include <bits/stdc++.h>
using namespace std;

const int N = 1010;
int dist[N][N];
int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};

// 网格 BFS 求最短路
void bfs(int sx, int sy, int n, int m) {
    memset(dist, -1, sizeof(dist));
    queue<pair<int,int>> q;
    q.push({sx, sy});
    dist[sx][sy] = 0;

    while (!q.empty()) {
        auto [x, y] = q.front(); q.pop();
        for (int d = 0; d < 4; d++) {
            int nx = x + dx[d], ny = y + dy[d];
            if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
            if (dist[nx][ny] != -1) continue;  // 已访问
            dist[nx][ny] = dist[x][y] + 1;
            q.push({nx, ny});
        }
    }
}

int main() {
    bfs(0, 0, 5, 5);
    cout << dist[4][4] << endl;  // 从(0,0)到(4,4)的最短距离
    return 0;
}

📖 要点说明

⚠️ 常见错误