My first instinct was to BFS from each walkable cell separately, which works but is obviously O((m*n)^2) in the worst case and they were not happy with that.
Use multi-source BFS starting from all store cells simultaneously, treating the grid as an unweighted graph. Initialize a distance matrix with 0 for stores and infinity for others, then propagate distances level by level to all walkable neighbors.
Pro tip: Mention that multi-source BFS is optimal because it computes distances in O(mn) time, and highlight how it naturally handles multiple stores without redundant work. Also, discuss edge cases like no stores or unreachable cells to show thoroughness.
Confirm grid dimensions, movement directions (4-way or 8-way), and what constitutes a walkable cell. Ask about input format and expected output.
Select multi-source BFS over alternatives like running BFS from each store (O(S*mn)) or Dijkstra (unnecessary for unweighted). Explain why BFS is optimal.
Create a distance matrix initialized to infinity, and a queue. Enqueue all store cells with distance 0.
While queue not empty, dequeue a cell, explore its walkable neighbors. If a neighbor's distance is infinity, set it to current distance + 1 and enqueue.
Return the distance matrix. Analyze time and space complexity as O(mn). Mention handling of unreachable cells (remain infinity).
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.