Navigate a Grid with A*
Find the fewest moves across a grid with A*: a min-heap ordered by f = g + h, with Manhattan distance as h.
The problem
Implement astar_path_length(grid, start, goal). grid is a rectangle of 0s (open cells) and 1s (walls), and start and goal are [row, col] positions of open cells. Each move goes one cell up, down, left or right onto an open cell. Return the fewest moves from start to goal, or -1 if goal cannot be reached. Solve it with A* search, using the Manhattan distance to goal as the estimate of the moves left. For example, in [[0, 1, 0], [0, 0, 0], [1, 0, 0]] the fewest moves from [0, 0] to [0, 2] is 4, going around the wall.
Starter code
def astar_path_length(grid, start, goal):
passPublic examples
grid = [[0, 1, 0], [0, 0, 0], [1, 0, 0]], start = [0, 0], goal = [0, 2]gives4grid = [[0, 1], [1, 0]], start = [0, 0], goal = [1, 1]gives-1
Solving it in the practice book needs an active paid plan.
Open it in the practice bookOne thought at a time.
