基于BFS的最短路径搜索[C++]

基于C++实现BFS的最短路径搜索时,可以使用STL中的优先队列priority_queue,优先队列按照小顶堆,即路径短的优先取出。其中节点可以用结构体定义,结构体中存储节点的位置、路径长度、最短路径的前序节点等信息,构建结构体的时候记得重载小于号,即可直接在优先队列中使用,具体代码如下:

其中,起点为左上角,终点为右下角,障碍物通过值设置为9999,可以根据题目的要求设置为一个非常大的值

#include<bits/stdc++.h>
using namespace std;
struct node{
  int x;
  int y;
  int prex;///前序节点x
  int prey;///前序节点y
  int val;///到该处路径的代价
  bool operator < (const node &x) const{
        return val > x.val;
  }
}S[16][16];
vector<vector<int> > array(16);
priority_queue<node> q;
int M,N;
void judge(int xadd, int yadd, int val, int px, int py){
    int x = px + xadd;
    int y = py + yadd;
    if(x<0||y<0||x>=M||y>=N){
      return;
    }
   // cout<<val<<" "<<array[x][y]<<" "<<S[x][y].val<<endl;
    if(val+array[x][y]<S[x][y].val){
       S[x][y].val = val+array[x][y];
       S[x][y].prex = px;
       S[x][y].prey = py;
       q.push(S[x][y]);
    }
    return;
}
void route(int x, int y){
    if(x<0||y<0){
        return;
    }
    route(S[x][y].prex, S[x][y].prey);
    if(!(x==0&&y==0)){
       cout<<" ";
    }
    cout<<"("<<x<<","<<y<<")";
}
int main(){
    while(cin>>M>>N){
        ///数组读取
        for(int i=0;i<M;i++){
            array[i].clear();
            array[i].resize(N);
            for(int j=0;j<N;j++){
               cin>>array[i][j];
               S[i][j].x = i;
               S[i][j].y = j;
               S[i][j].prex = -1;
               S[i][j].prey = -1;
               S[i][j].val = 9999;
            }
        }
        ///最短路
        while(!q.empty()) q.pop();
        S[0][0].val = 0;
        q.push(S[0][0]);
        while(!q.empty()){
          //  cout<<q.top().x<<" "<<q.top().y<<endl;
            node temp = q.top();
            q.pop();
            judge(-1, 0, temp.val, temp.x, temp.y);
            judge( 1, 0, temp.val, temp.x, temp.y);
            judge( 0, 1, temp.val, temp.x, temp.y);
            judge( 0,-1, temp.val, temp.x, temp.y);
        }
        if(S[M-1][N-1].val>5000){
          cout<<"-1"<<endl;
        }else{
          route(M-1,N-1);
          cout<<endl;
        }
    }
}
/*
7 5
0 1 1 1 1
8 1 4 9 7
4 9999 9999 5 1
1 1 6 9999 6
2 9999 3 9999 4
4 3 9999 5 3
4 2 3 2 0



*/

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
好的,以下是一个基于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 dis[MAXN]; // 存储起点到每个节点的距离 bool vis[MAXN]; // 记录每个节点是否被访问过 int head[MAXN], to[MAXN<<1], nxt[MAXN<<1], edge[MAXN<<1], cnt; // 邻接表存图 void addEdge(int u, int v, int w) { to[++cnt] = v; nxt[cnt] = head[u]; edge[cnt] = w; head[u] = cnt; } void bfs(int start) { memset(dis, INF, sizeof(dis)); // 初始化距离为无穷大 memset(vis, false, sizeof(vis)); // 初始化所有节点都未被访问过 dis[start] = 0; // 起点到自己的距离为0 vis[start] = true; // 起点已经访问过了 queue<int> q; // 定义一个队列,用于广度优先搜索 q.push(start); // 将起点加入队列 while (!q.empty()) { int u = q.front(); q.pop(); // 取出队首节点 for (int i = head[u]; i; i = nxt[i]) { int v = to[i], w = edge[i]; if (!vis[v]) { // 如果这个节点未被访问过 dis[v] = dis[u] + w; // 更新距离 vis[v] = true; // 标记为已访问 q.push(v); // 将其加入队列 } } } } int main() { cin >> n >> m; for (int i = 1; i <= m; ++i) { int u, v, w; cin >> u >> v >> w; addEdge(u, v, w); addEdge(v, u, w); // 无向图需要加两条边 } int start; cin >> start; bfs(start); for (int i = 1; i <= n; ++i) { if (dis[i] != INF) { cout << start << " 到 " << i << " 的最距离为:" << dis[i] << endl; } } return 0; } ``` 这段代码使用邻接表存图,时间复杂度为O(n+m),其中n表示节点数,m表示边数。在实际使用中,可以根据自己的需求进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值