A* 搜索中的"Jumping"
"Jumping" in A* search
我正在尝试实现*搜索算法,这是我的尝试:
void Map::findPath(Robot robot, Model target, vector<Node> nodeList, vector<Node> &closedList) {
Node targetNode = target.getCurrentNode();
Node startNode = robot.getCurrentNode();
vector<Node> openList;
openList.push_back(startNode);
startNode.setG(0);startNode.setH(0);startNode.setF(0);
int i = 0;
while (!openList.empty()) {
Node currentNode = nodeWithLowestFScore(openList);//always returns the most recent one
/*for ( int i = 0; i < openList.size(); i++){
cout << "X: " << openList[i].getX() << " Y: " << openList[i].getY() <<
" G: " << openList[i].getG() << " M: " << openList[i].getH() << endl;
}*/
cout << i++ << ". " << currentNode.getX() << " " << currentNode.getY() << " G: " <<
currentNode.getG() << " M: " << currentNode.getH() << endl;
closedList.push_back(currentNode);
removeFromVector(openList, currentNode);
if (inVector(closedList, targetNode))
break;
vector<Node> adjacentNodes;
currentNode.getWalkableAdjacentNodes(nodeList, adjacentNodes);
for ( int i = 0; i < adjacentNodes.size(); i++){
if (inVector(closedList, adjacentNodes[i]))
continue;
if (!inVector(openList, adjacentNodes[i])){
adjacentNodes[i].setParent(¤tNode);
adjacentNodes[i].setG(currentNode.getG() +1);//distance is always 1 between adjacent nodes
adjacentNodes[i].setH(adjacentNodes[i].getDistance(targetNode, 'm'));//heuristic as manhattan
adjacentNodes[i].setF(adjacentNodes[i].getG() + adjacentNodes[i].getH());
openList.push_back(adjacentNodes[i]);
}
if (inVector(openList, adjacentNodes[i])){//update if it's in the list already
//?
}
}
}
}
我认为功能的名称是自我解释的,所以我不会进入它们。无论如何,在我的示例输出中,我试图从(x:0,y:-2)到(x:-7,y:6)
- 0 -2
- -1 -2
- -2 -2
- -3 -2
- -3 -1
- -3 0
- -4 0
- -5 0
- -5 1
- -5 2
- -5 3
- -5 4
- -6 4
- -7 4
- -5 5
- -5 6
- -3 1
- -3 2
- -3 3
- -3 4
- -2 4
- -2 2
- -4 6
- -5 7
- -8 4
- -4 4
- -5 -1
- -1 -3
- 1 -2
- 2 -2
- -1 -4
- -2 -4
- -3 -4
- -5 -2
- -9 4
- -9 5
- -9 6
- -8 6
- -7 6
直到第14行,情况似乎都很好,但随后突然跳到(5,5)。任何帮助都非常感谢。
访问节点的顺序不一定是您的最短路径。据我所知,该算法运行良好。观察到节点-5 4
在第12行中访问,因此-5 5
只是第15行中访问的该节点的邻居。要获取最短路径,您应该将最终节点的括号跟踪回到末端的初始节点算法。
相关文章:
- 有根的二进制搜索树.保留与其父级的链接
- 在C++中搜索嵌套多映射值
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 二进制搜索树叶数问题
- 为什么二进制搜索在我的测试中不起作用
- 正在尝试重载二进制搜索树分配运算符
- c++binary_search函数排序数组(流行名称搜索)出现问题
- 向量上的线性搜索
- 如何在动态数组上使用搜索函数
- 对于MacOS上的G++,如何添加默认的include目录/usr/local/include和默认的库搜索路径/usr
- cmake:添加要搜索头文件的目录
- 使用C++创建特殊的二叉搜索树
- 在C++的字符串中搜索和删除某些字符
- std::unordered_map 搜索算法是如何实现的?
- 使用不变量来确定二分搜索中的边界条件
- 二叉搜索如何比线性搜索更快?
- 按边长度递归搜索图中所有可行路径
- QStackWidget - 按名称搜索
- 在递归二叉搜索树中搜索
- A* 搜索中的"Jumping"