Problem
In a grid with open and blocked cells, find the shortest four-directional path between a source and a target. Return -1 if the target cannot be reached.
Worked examples
Input: grid = [[1,1],[0,1]], source = (0,0), target = (1,1)
Output: 2
Move right, then down; both cells are open.
Hints
Hint 1
Every move has the same cost.
Hint 2
A breadth-first search explores paths by increasing distance.
Solution approach
- Enqueue the source at distance zero and mark it visited immediately.
- For each popped cell, enqueue its open, unvisited four-directional neighbours at distance + 1.
- Return when the target is reached; if the queue empties, return -1.
Complexity
O(rows × columns) time and space.
Report & practice notes
Restated practice version with original examples and explanation. The source is a candidate account, not an official question paper; assessment details can vary. Difficulty is our editorial estimate.
Read the candidate’s source report ↗