C++ C++;,*搜索程序不支持';t正确计算从出口到出口的距离

C++ C++;,*搜索程序不支持';t正确计算从出口到出口的距离,c++,a-star,C++,A Star,我一直与VisualStudio有问题,所以提前表示歉意,但我一直在使用在线网站进行编译,不能使用断点,就像我现在很想在这个程序中使用断点一样 我还在研究它,但是现在得到A*星的H值,即距离出口的距离,似乎没有正确显示/计算 它工作到一定程度,然后它似乎开始反向工作 我相信问题出在我的搜索中,虽然它给出了节点的值,并且它显示正确,但并不像预期的那样。第110点以后。例如: int start = 0; bool search = true; aStarArray[myCoord.endY][my

我一直与VisualStudio有问题,所以提前表示歉意,但我一直在使用在线网站进行编译,不能使用断点,就像我现在很想在这个程序中使用断点一样

我还在研究它,但是现在得到A*星的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
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,我们将不胜感激,或者只是一般的任何错误。 谢谢。

您的支票

if (myCoord.y + 1 <= HEIGHT)

if(mycord.y+1您尝试过吗?您不能在联机编译器上调试的事实并不意味着您在发布之前不再承担调试的责任。我们不是免费的众包调试器。我们不是来为您免费调试的。找到一种绕过您的约束的方法,以便您可以调试代码。Visual Studio可能会e免费获得。我已经尽了最大努力。我花了几个小时,尝试了多种不同的实现,但没有一种实现比这更接近。我唯一的警告是两个未使用的参数,但我知道这是为什么,这不是问题所在。正如我所解释的,我想使用断点,但不能,这只是另一回事否则我会这么做的。这是我最后的选择,因为很可能这是我不理解的东西,所以对我来说继续检查它是毫无意义的,因为我假设某个东西是正确的,而不是错误的。顺便说一句,启发式计算不是真距离,而是快速下限估计。看起来你的启发式是曼哈顿距离?为什么不直接使用abs(x-exit.x)+abs(y-exit.y)
if (myCoord.y + 1 <= HEIGHT)
if (myCoord.y + 1 < HEIGHT)