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

时间:2015-12-21 22:05:48

标签: c++ a-star

我一直在与Visual Studio有问题,所以提前道歉,但我一直在使用在线网站进行编译,不能使用断点,就像我喜欢的那样现在正在这个计划上。

我还在努力,但是现在得到A * Star的H值,离出口的距离,似乎没有正确显示/计算。

它可以达到一定程度,然后似乎开始向后工作。

我相信问题出现在我的搜索中,虽然它给节点提供了它们的价值,并且它正确地显示但是没有按预期显示。点110向前。 EG:

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/8iykn

如果有人有一些建议来解决获得H的问题,那将会受到赞赏,或者只是一般的错误。 感谢。

1 个答案:

答案 0 :(得分:0)

您的支票

if (myCoord.y + 1 <= HEIGHT)

错了。它应该是

if (myCoord.y + 1 < HEIGHT)

WIDTH

相同