c++ - 如何使用 BFS 重建路径
问题描述
我了解到广度优先搜索是一种可用于计算两个节点之间最短距离的算法。我在下面的代码中实现了 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;
}
我的问题是,是否存在一种有效的方法来重建两个节点之间的路径而不运行另一种算法?如果没有,是否有另一种可以重建路径的最短路径算法?
解决方案
要使用 重建路径breadth-first search
,您可以保留一个vector
跟踪每个节点的父节点的父节点。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)
在您的情况下,parent
应该是一个map <QItem, QItem>
,跟踪每个QItem's
父母。
推荐阅读
- html - CSS将整个相对主体水平居中,没有包装器或滚动条?
- regex - 在python中使用正则表达式进行模式匹配没有给出预期的操作
- powershell - 如何删除空格并仅显示文本(如 awk)?
- python - 使用相对路径获取python中的所有png文件
- mysql - mariadb 每天增加磁盘使用量并在停止 mariadb 守护进程时回收空间
- java - 如何从 Firebase 实时数据库 Java 中的子节点检索数组数据?(Firebase 回收器适配器)
- php - 启用 fileinfo 但仍然收到错误 WordPress 插件要求
- c - 如何在 ftrace 中打印 trace_printk 的完整跟踪文件?
- python - 按行连接 3D numpy 数组
- javascript - 我需要在引导多选时触发离开事件,但我也不能点击甚至不工作