【问题标题】:Grid-graph path finder code using BFS in c++在 C++ 中使用 BFS 的网格图路径查找器代码
【发布时间】:2022-06-10 23:03:33
【问题描述】:

我正在尝试在 C++ 中编写 BFS 算法以在网格中查找单元格,但是代码没有给出任何输出(只是空白)。该代码类似于在线给出的所有标准代码,但我无法理解它是如何不起作用的。

class grid graph是声明图形的类,是一个二维数组,0为路径,1为障碍物

pathfinder 是一种查找路径的方法,使用广度优先搜索算法 它有自己的辅助函数添加邻居来添加邻居

#include<iostream>
#include <bits/stdc++.h>
using namespace std ;
class grid_graph{
    public:
    vector<vector<int>> A;
    grid_graph(vector<vector<int>> a){
        A = a;
    }
    int N_rows = A.size();
    int N_cols = A[0].size();
    
    void pathfinder(int src_r,int src_c,int dest_r,int dest_c);
    void neighbour_adder(int r,int c,queue<int>& R,queue<int>& C,vector<vector<bool>>& visited);//bool visited[][N_cols]
};
void grid_graph::pathfinder(int src_r,int src_c,int dest_r,int dest_c){
    queue<int> R;
    queue<int> C;
    R.push(src_r);
    C.push(src_c);
    // bool visited[N_rows][N_cols]{};
    vector<vector<bool>> visited;
    for(int i=0; i<N_rows; i++){
        for(int j=0; j<N_cols; j++){
            visited[i][j]=false;
        }
    }
    // visited[src_r][src_c] = true;
    while(!R.empty()){
        cout<<R.front()<<" "<<C.front()<<endl;
        if(R.front()==dest_r && C.front()==dest_c){
            cout<<"reached"<<endl;
        }
        visited[R.front()][C.front()]=true;
        neighbour_adder(R.front(),C.front(),R,C,visited);
        R.pop();
        C.pop();
    }
}
void grid_graph::neighbour_adder(int r,int c,queue<int>& R,queue<int>& C,vector<vector<bool>>& visited){//bool visited[][N_cols]
    // assuming only up down left right motion possible 
    int d1[4] = {0,0,+1,-1};
    int d2[4] = {+1,-1,0,0};
    for(int i=0; i<4; i++){
        int r_next = r + d1[i];
        int c_next = c + d2[i];
        if(r_next<0 || c_next<0 || r_next>=N_rows || c_next>=N_cols){
            continue;
        }
        // I have taken 1 as obstacle 0 as not obstacle 
        if(A[r_next][c_next]==1 || visited[r_next][c_next]==true){
            continue;
        }
        R.push(r_next);
        C.push(c_next);
    }

}

int main(){
    
    grid_graph g2( {{ 0, 0, 0 },
                    { 0, 1, 0 },
                    { 0, 0, 0 } });
    g2.pathfinder(0,0,2,2);
    return 0;
}

【问题讨论】:

  • 看看valgrindvisited[i][j]=false; 的看法,然后从那里开始调试。来自the documentation 的简单提示:与 std::map::operator[] 不同,此运算符从不向容器中插入新元素。 通过此运算符访问不存在的元素是未定义的行为。

标签: c++ oop graph breadth-first-search


【解决方案1】:

这是不正确的。

// bool visited[N_rows][N_cols]{};
vector<vector<bool>> visited;
for(int i=0; i<N_rows; i++){
    for(int j=0; j<N_cols; j++){
        visited[i][j]=false;
    }
}

这是初始化指定大小的二维向量的正确代码。

vector<vector<bool> > visited(
    N_rows,
    vector<bool>(N_cols, false));

这是有问题的

grid_graph(vector<vector<int>> a){
    A = a;
}
int N_rows = A.size();
int N_cols = A[0].size();

我会写:

grid_graph(vector<vector<int>> a)
: A( a )
{}

int colCount() const
{
    return A[0].size();
}
int rowCount() const
{
    return A.size();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多