The base BFS from all destination cells at once is pretty standard, but the obstacle wrinkle tripped me up initially.
Use multi-source BFS starting from all destination cells simultaneously, treating obstacles as passable with a cost of 1. After computing distances, for each obstacle cell, find the minimum distance to any destination by considering its neighbors' distances plus one.
Pro tip: Clarify with the interviewer whether obstacles are passable for distance calculation; if not, you may need to compute distances through obstacles using a different method like Dijkstra with obstacles having a cost. Always discuss trade-offs and edge cases.
Clarify that obstacles are passable for distance computation, and that distance is measured as the number of steps moving up/down/left/right. Confirm if diagonal moves are allowed.
Enqueue all destination cells with distance 0. Use a queue for BFS and a distance matrix initialized to infinity.
During BFS, allow moving into obstacle cells as if they were normal cells, updating distances. This computes the shortest distance from each cell to the nearest destination, ignoring obstacles as barriers.
If the problem requires that obstacles are not passable, then after BFS, for each obstacle cell, compute its distance as 1 + min(neighbor distances) if any neighbor is reachable. Otherwise, mark as unreachable.
Ensure all cells have a valid distance (or -1 if unreachable). Discuss time and space complexity: O(m*n) time and space.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.