BFS from the start, track distances, return the distance at the goal cell.
Model the maze as an unweighted graph and use BFS from the start cell to find the shortest path to the goal. Track visited cells to avoid cycles and return the distance when the goal is reached, or -1 if the queue empties.
Pro tip: Mention that BFS is optimal for unweighted grids and that you can optimize space by using a 2D distance array or by marking visited cells in-place if mutation is allowed.
Confirm grid dimensions, wall representation, and whether start/goal are guaranteed to be empty. Discuss edge cases like start equals goal, no path, or invalid inputs.
Explain that BFS is ideal for shortest path in unweighted graphs. Define the state as (row, col) and use a queue to process cells level by level.
Initialize queue with start, mark visited, and set distance to 0. While queue not empty, dequeue, check if goal, and enqueue valid unvisited neighbors with distance+1.
If goal is reached, return its distance. If queue empties without reaching goal, return -1.
State time complexity O(R*C) and space O(R*C). Mention possible optimizations like bidirectional BFS or using a visited set vs. in-place marking.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.