C++ 如何使用BFS重建路径

C++ 如何使用BFS重建路径,c++,algorithm,breadth-first-search,C++,Algorithm,Breadth First Search,我了解到宽度优先搜索是一种算法,可以用来计算两个节点之间的最短距离。我在下面的代码中实现了BFS算法: #define N 4 #define M 4 // QItem for current location and distance // from source location class QItem { public: int row; int col; int dist; QItem(int x, int y, int w) :

我了解到宽度优先搜索是一种算法,可以用来计算两个节点之间的最短距离。我在下面的代码中实现了BFS算法:

#define N 4 
#define M 4 

// QItem for current location and distance 
// from source location 
class QItem {
public:
    int row;
    int col;
    int dist;
    QItem(int x, int y, int w)
        : row(x), col(y), dist(w)
    {
    }
};


int minDistance(char grid[N][M])
{
    QItem source(0, 0, 0);

    // To keep track of visited QItems. Marking 
    // blocked cells as visited. 
    bool visited[N][M];
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++)
        {
            if (grid[i][j] == '0')
                visited[i][j] = true;
            else
                visited[i][j] = false;

            // Finding source 
            if (grid[i][j] == 's')
            {
                source.row = i;
                source.col = j;
            }
        }
    }

    // applying BFS on matrix cells starting from source 
    queue<QItem> q;
    q.push(source);
    visited[source.row][source.col] = true;
    while (!q.empty()) {
        QItem p = q.front();
        q.pop();

        if (grid[p.row][p.col] == 'd')
            return p.dist;

        if (p.row - 1 >= 0 && visited[p.row - 1][p.col] == false) {
            q.push(QItem(p.row - 1, p.col, p.dist + 1));
            visited[p.row - 1][p.col] = true;
        }

        if (p.row + 1 < N && visited[p.row + 1][p.col] == false) {
            q.push(QItem(p.row + 1, p.col, p.dist + 1));
            visited[p.row + 1][p.col] = true;
        }

        if (p.col - 1 >= 0 && visited[p.row][p.col - 1] == false) {
            q.push(QItem(p.row, p.col - 1, p.dist + 1));
            visited[p.row][p.col - 1] = true;
        }

        if (p.col + 1 < M && visited[p.row][p.col + 1] == false) {
            q.push(QItem(p.row, p.col + 1, p.dist + 1));
            visited[p.row][p.col + 1] = true;
        }
    }
    return -1;
}

int main()
{
    char grid[N][M] = { { '0', '*', '0', 's' },
                        { '*', '0', '*', '*' },
                        { '0', '*', '*', '*' },
                        { 'd', '*', '*', '*' } };

    cout << minDistance(grid);
    return 0;
}
#定义N 4
#定义M4
//当前位置和距离的QItem
//从源位置
课堂教学{
公众:
int行;
int col;
国际区;
QItem(整数x,整数y,整数w)
:行(x)、列(y)、区(w)
{
}
};
int minDistance(字符网格[N][M])
{
QItem源(0,0,0);
//跟踪访问的Qitem。标记
//访问时被阻塞的单元格。
布尔访问[N][M];
对于(int i=0;i=0且访问了[p.row-1][p.col]==false){
q、 推(QItem(p.row-1,p.col,p.dist+1));
已访问[p.row-1][p.col]=true;
}
if(p.row+1=0且访问了[p.row][p.col-1]==false){
q、 push(QItem(p.row,p.col-1,p.dist+1));
访问[p.row][p.col-1]=真;
}
如果(p.col+1cout要使用
宽度优先搜索重构路径
,您可以保留跟踪每个节点的父节点的父节点
向量
。在BFS完成运行后,您可以从结束节点追溯到开始构建路径

下面的一些代码说明了这一点:

while(!Q.empty){
    int curr = Q.front();
    Q.pop();

    if(curr == destination){break;}

    for(int item : adj[curr]){ // for every element adjacent to curr
        if(!visited[item]){
            visited[item] = true;
            parent[item] = curr; // connects curr to item
            Q.push(item);
        }
    }
}

// trace from the destination back to the start
int tracer = destination;
vector <int> path = {tracer};

while(tracer != start){
    tracer = parent[tracer];
    path.push_back(tracer);
}

// note that this constructs path in reverse order (from end to start)

while(!Q.empty){
int curr=Q.front();
Q.pop();
如果(curr==目的地){break;}
for(int item:adj[curr]){//for与curr相邻的每个元素
如果(!已访问[项目]){
已访问[项目]=真;
父项[item]=curr;//将curr连接到项
Q.推动(项目);
}
}
}
//从目的地追溯到起点
int tracer=目的地;
向量路径={tracer};
while(跟踪器!=开始){
跟踪器=父项[跟踪器];
路径。推回(跟踪器);
}
//请注意,这以相反的顺序构造路径(从端到端)

在您的情况下,
parent
应该是一个
map
,跟踪每个
QItem的父项。

请参见此处(重复):我已经看到了这一点,但我无法在我的代码中解决这一问题。如果您能说明解决方案不起作用的原因,这将非常有用。我只知道我需要路径中该节点的父节点,我可以通过从目标节点向后遍历来检索路径-但是如何在不运行其他算法的情况下将其添加到代码中?