What if you may break one wall? Search over the cell plus the walls broken so far
A classic follow-up: the fewest steps when you may knock down one wall.
Predict
You may break one rock on MAZE. How many steps from (0,0) to (0,3) now?
MAZE, from (0,0) to (0,3)
around the rock
0.#7..1.#6#.2345#.
through it, one break
0123....#.#.....#.
Around the rock is 7 steps. Breaking the rock at (0,2), in red, makes it 3.
For the fewest steps when you may knock down one wall, the cell alone isn't the state anymore. Two visits to the same cell differ if one has already used its break. Search over (r, c, walls_broken). Entering a wall adds one, and a state over the limit is dropped. The seen-set holds states, so it can grow to the limit plus one entries per cell. On the maze above, (0, 0) to (0, 3) takes 7 steps around the wall and 3 through it.
In the editor, write fewest_with_breaks(grid, start, goal, limit): the fewest steps when you may break up to limit rocks. On MAZE it gives 7 with no break and 3 with one.
Pick an answer above to see the rest
Instructions
1
Go one level per pass over states (r, c, broken). Entering a # adds one to broken, and a state past limit is dropped.
2
Keep the seen-set on states, so a visit that has broken fewer rocks is never skipped. Return the steps when you pop goal.