27/32
Part 7 of 7 · Follow-upsLearn and do

What do you return when nothing reaches a cell? Agree on it before you code

-1 is the usual answer for a count. For a map of distances, leave the cell out or give it math.inf, and say which. Keep 0 and -1 apart: 0 means done at the start, -1 means never. Test both, plus a grid with no source at all.

How do you get the path, not just its length? Store each cell's parent

Store parent[cell] = (r, c) in the same step that marks a cell. Then walk back from the goal to the start, and reverse. It's one more dict, still O(RC)O(RC). On the maze, the walk back from the corner gives this 11-step path:

The path read from the parents, from (0,0) to G
path
↓.#→→↓↓.#↑#↓→→→↑#G

Each arrow points to the next cell. G is the goal, (2,5).

In the editor, finish path_to(grid, start, goal): store each cell's parent as you queue it, then walk back from goal. Return None when nothing reaches the goal.