C++,A* 搜索程序无法正确计算到出口的距离
C++, A* Search Program doesn't calculate distance from exit correctly
我在Visual Studio方面遇到了问题,所以提前道歉,但我一直在使用在线网站进行编译,不能使用断点,因为我现在很想在这个程序上使用断点。
我仍在研究,但现在获取A*Star的H值,即离出口的距离,似乎无法正确显示/计算。
它在一定程度上起作用,然后似乎开始倒退。
我相信问题出在我的搜索中,尽管这给了节点它们的值,并且它显示正确,但并没有像预期的那样。点110向前。例如:
int start = 0;
bool search = true;
aStarArray[myCoord.endY][myCoord.endX].h = 0; // End cell coord, written as 0 for the H array. (0 distance to exit)
while (search == true) // Will end as soon as all nodes have been valued.
{
for (myCoord.y = 0; myCoord.y < HEIGHT; ++myCoord.y)
{
for (myCoord.x = 0; myCoord.x < WIDTH; ++myCoord.x)
{
if (aStarArray[myCoord.y][myCoord.x].h == start) // Is value we're looking for.
{
if (myCoord.y + 1 <= HEIGHT)
{
if (aStarArray[myCoord.y+1][myCoord.x].h == -1) // Not blocked, but not distanced yet and is a valid cell.
{
aStarArray[myCoord.y+1][myCoord.x].h = start + 1; // Then give it a value of parent cell + 1. (start + 1)
search = false;
}
}
if (myCoord.y - 1 >= 0)
{
if (aStarArray[myCoord.y-1][myCoord.x].h == -1)
{
aStarArray[myCoord.y-1][myCoord.x].h = start + 1;
search = false;
}
}
if (myCoord.x + 1 <= WIDTH)
{
if (aStarArray[myCoord.y][myCoord.x+1].h == -1)
{
aStarArray[myCoord.y][myCoord.x+1].h = start + 1;
search = false;
}
}
if (myCoord.x - 1 >= 0)
{
if (aStarArray[myCoord.y][myCoord.x-1].h == -1)
{
aStarArray[myCoord.y][myCoord.x-1].h = start + 1;
search = false;
}
}
}
}
}
start = start + 1;
if (search == false) // A change was made to a node on this loop of the array.
{
search = true; // Then assume more nodes await values, keep searching.
// Now we're moving on to one of those new distanced cells, so the parent changes and thus search terms.
// Start gets +1, so it's start searching for the new cells. And then it'll distance their neighbour cells to start+1, cycle repeats.
}
else // No changes.
{
search = false; // No need to search anymore, theoretically.
}
}
}
目前的结果是:
Get H values for every node
6 7 6 5 4
5 6 5 4 3
4 5 4 3 2
3 4 3 2 1
2 3 2 1 0
当我希望它是这样的时候,例如:
Get H values for every node
8 7 6 5 4
7 6 5 4 3
6 5 4 3 2
5 4 3 2 1
4 3 2 1 0
完整的代码可以在这里找到:cpp.sh/8ykn
如果有人有一些建议来解决得了H的问题,我们将不胜感激,或者只是一般的错误。谢谢
您的支票
if (myCoord.y + 1 <= HEIGHT)
是错误的。应该是
if (myCoord.y + 1 < HEIGHT)
与WIDTH
相同
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 递归函数计算序列中的平方和(并输出过程)
- (C++)分析树以计算返回错误值的简单算术表达式
- 我的字符计数代码计算错误.为什么
- 在计算中使用二的幂有多有利可图
- 如何计算文件中的"columns"数?
- 计算排序向量的向量中唯一值的计数
- 如何使用 std::累积在 C++ 中计算总和立方体
- 使用Qt C++计算类似Git的SHA1哈希
- OpenCV C++.快速计算混淆矩阵
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- C++如何计算用户输入的数字中的偶数位数
- 如何计算数据类型的范围,例如int
- 类似枚举的计算常量
- 计算每个节点的树高,帮助我解释这个代码解决方案
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 计算缩放多边形的比例,得到给定的多边形面积
- 在C++中如何在没有pow的情况下进行基础计算
- 计算平均值,不包括上次得分
- C++,A* 搜索程序无法正确计算到出口的距离