簡體   English   中英

在 C++ 中使用 BFS 的網格圖路徑查找器代碼

[英]Grid-graph path finder code using BFS in c++

我正在嘗試在 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;
}

編輯1:由於解決方案,代碼現在可以完美運行,這里是其他任何需要它的人的工作代碼:

/

/////////////////////////////////////////////////
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();
    /////////////////////////////////////////////////////////////// from SO
    grid_graph(vector<vector<int>> a): A( a ){}

    int colCount() const
    {
        return A[0].size();
    }
    int rowCount() const
    {
        return A.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;
    //     }
    // }
    int N_rows = rowCount();
    int N_cols = colCount();
    ///////////////////////////////////from stackexchange 
    vector<vector<bool> > visited(N_rows,vector<bool>(N_cols, false));
    /////////////////////////////////
    // visited[src_r][src_c] = true;
    while(!R.empty()){
        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};
    int N_rows = rowCount();
    int N_cols = colCount();
    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);
    }

}
/////////////////////////////////////////////////////////////////////////////////////////////////
/* Dijkstra algorithm is a greedy algorithm used for shortest path in non negative weighted
 graphs . we have lazy , eager , d ary heap and fibonacci heap versions of it . 
*/
/*Topological sort , topsort done using dfs over all unvisited nodes and putting them in reverse 
in an array (which is the topsort output ) , can only be done over directed acyclic graph  DAG*/
int main(){
    // cout<<"hello world"<<endl;
    unweighted_graph g1;
    /////////////////////////////////////
    // g1.addedge_undirected(1,2);
    // g1.addedge_directed(2,3);
    // g1.print_graph();
    // print graph , directed and undirected edges functions are working properly 
    ////////////////////////////////////

    ////////////////////////////////////
    // graph taken from https://www.geeksforgeeks.org/breadth-first-search-or-bfs-for-a-graph/
    // g1.addedge_directed(0, 1);
    // g1.addedge_directed(0, 2);
    // g1.addedge_directed(1, 2);
    // g1.addedge_directed(2, 0);
    // g1.addedge_directed(2, 3);
    // g1.addedge_directed(3, 3);
    // g1.BFS_iterative(2);
    // gave output 2 0 3 1 which is correct hence iterative BFS is working properly 
    // g1.BFS_recursive(2);
    // gave output 2 0 3 1 which is correct hence recursive BFS is working correctly 
    ////////////////////////////////////
    // g.addedge_directed(0, 1);
    // g.addedge_directed(0, 2);
    // g.addedge_directed(1, 2);
    // g.addedge_directed(2, 0);
    // g.addedge_directed(2, 3);
    // g.addedge_directed(3, 3);
    // g.DFS_iterative(0);
    // gave output 2 3 0 1 for 2 , 3 for 3 , 0 2 3 1 for 0 hence it is working 
    // g.DFS_recursive(0);
    // gave output 2 0 1 3 for 2 , 3 for 3 , 0 1 2 3 for 0 hence it is working 
    ////////////////////////////////////
    grid_graph g2( {{ 0, 0, 0 },
                    { 0, 1, 0 },
                    { 0, 0, 0 } });
    g2.pathfinder(0,0,2,2);

    return 0;
}

我的 VS 代碼顯示 bits/std.h not found 錯誤,我不知道這些庫在我的電腦上的位置以及如何獲取資源,所以任何人都可以幫忙

編輯 2 - 代碼現在可在 github 上獲得,以及 BFS DFS Dijkstra 的代碼在https://github.com/ayush-agarwal-0502/Graph-Algorithms-

這是不正確的。

// 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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM