Manhattan algorithm for the pathfinding algorithm