Challenge
A grid uses 0 for an open cell and 1 for a blocked cell. Return the minimum number of moves from start=[row,column] to goal. Each move goes one cell up, down, left or right; diagonal moves are forbidden. Return -1 if either endpoint is blocked or no path exists.
Input and output contract
A nonempty rectangular grid with at most 40,000 cells; both coordinates are in bounds. If start equals goal and the cell is open, return 0. A move count is an edge count, not a count of visited cells.
All tests follow this contract. You do not need to validate malformed input. Return the requested result from grid_shortest_path; printed output is not your answer. Standard-library tools are allowed. Implement your solution in Python 3.12.
Target complexity: O(rows × columns) time and auxiliary space
Return values are checked by type. Use the exact function name. Print output is not your answer. Tests have time and memory limits.
Public examples
Arguments
[[[0, 1, 0], [0, 0, 0]], [0, 0], [0, 2]]
Expected return
4
Arguments
[[[0]], [0, 0], [0, 0]]
Expected return
0
Arguments
[[[0, 1], [1, 0]], [0, 0], [1, 1]]
Expected return
-1
Hint
Every move has equal cost, so process reachable cells in increasing distance with a queue.
The worked solution unlocks after completion, or with Premium access.