← 返回 google 的题目列表Rotten Oranges / Multi-Source BFS (taxis)
类型:qbank
LC 542 / 994 variant — for every cell, distance to the nearest active source (a 'taxi'). Multi-source BFS is the canonical solution.
Requirements
Grid where some cells are sources (taxis / rotten oranges / gates); compute distance from every other cell to the nearest source.
Multi-source BFS: enqueue all sources at distance 0 simultaneously, expand layer by layer.
Follow-up reported: handle moving sources / time-varying placement (recompute periodically vs incremental update).
Examples
Grid [[2,1,1],[1,1,0],[0,1,1]] (rotting-oranges variant): return number of minutes until all reachable 1s become 2 (4).
Taxi variant: for each empty cell, distance to nearest taxi.
Notes
BFS from all sources simultaneously is O(mn); per-source BFS is O(mn × #sources).
The moving-source follow-up trades exactness for amortized cost — common answer is periodic rebuild with bounded staleness.
Preparation
Drill multi-source BFS from memory in under 10 min.
Prep a 2-minute pitch on incremental-vs-rebuild trade-off for the moving-source follow-up.
Practice the rotting-oranges / walls-and-gates / 01-matrix triplet — all share the multi-source BFS skeleton.