以下是引用BlueGuy在2011-9-25 13:07:23的发言:
我刚才也写了一个, 不过我只对4个方向搜索
h = f + g
// 结点初始化
void AStarNodeInit(AStartListNode* current, AStartListNode* father, int x, int y, int endX, int endY)
{
current->x = x;
current->y = y;
current->father = father;
current->next = NULL;
if (father != NULL)
{
current->f = father->f + 1;
}
else
{
current->f = 0;
}
current->g = abs(endX - x) + abs(endY - y);
current->h = current->f + current->g;
}
你在搜索8个方向的时候,写的繁琐了
int offsetX[] = { 0, -1, 0, 1 };
int offsetY[] = { -1, 0, 1, 0 };
for (i = 0; i < sizeof(offsetX); i++)
{
x = current->x + offsetX;
y = current->y + offsetY;
if (x < 0 || x >= mapWidth || y < 0 || y >= mapHeight)
{
continue;
}
}
代码的正确性可以保证,置顶一下,一起学习学习 laoyang 的代码.,
又见版主,求qq号~~