Вопрос

My a* path finding algorithm only works for certain cases but I don't understand why. Every node in my grid is walkable so in theory every path should work. I believe the error is in this line:

 PathFindingNode *neighbor = NULL;
            if ((y > 0 && x > 0) && (y < gridY - 1 && x < gridX - 1))
             neighbor = [[grid objectAtIndex:x + dx] objectAtIndex:y +dy];
Это было полезно?

Решение

In function -(void)addNeighbors:, the line

if ((y > 0 && x > 0) && (y < gridY - 1 && x < gridX - 1))
    neighbor = [[grid objectAtIndex:x + dx] objectAtIndex:y +dy];

has bug because if curNode is on boundary, it does not add neighbors to the queue. So that the algorithm will never reach endNode in the four corners (i.e. [0,0], [gridX-1,0], [0,gridY-1], [gridX-1,gridY-1]).

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top