
D-BFS 双向广度优先搜索
从起点和终点同时开始搜索,直到两个搜索的点相交,得到最短路径
Code:
// D-BFS
//by:MuQY
#include <iostream>
#include <algorithm>
#include <string.h>
#include <queue>
#include <string>
using namespace std;
int stmap[10005]; // 标记每一个情况下走了几步
int vis[10005]; // 标记每个情况下有没有被访问过,是正向还是逆向
string s, e;
struct Node
{
int num[4];
int step;
};
queue<Node> q, p; // q从s开始,p从e开始
int get_Num(int c[])
{
int tmp = 0;
for (int i = 0; i < 4; i++)
{
tmp *= 10;
tmp += c[i];
}
return tmp;
}
void bfs()
{
memset(vis, 0, sizeof(vis));
memset(stmap, 0, sizeof(stmap));
Node a, b;
int tmp;
while (!q.empty() || !p.empty())
{
if (!q.empty()){
a = q.front();
q.pop();
for (int i = 0; i < 4; i++){
b = a;
b.num[i] = a.num

本文详细介绍了如何使用C++编写D-BFS算法,从两个起点(s和e)同时开始搜索,找到并输出两点之间的最短路径。代码展示了如何处理节点、队列操作以及路径标记。
最低0.47元/天 解锁文章
831

被折叠的 条评论
为什么被折叠?



