C++,A* 搜索程序无法正确计算到出口的距离

C++, A* Search Program doesn't calculate distance from exit correctly

本文关键字:计算 出口 距离 搜索 程序 C++      更新时间:2023-10-16

我在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 相同