Rotting Oranges, traced one minute at a time

This one is BFS, not recursion. Every rotten orange goes into a queue at the start. Each pass over the queue is one minute: it pops exactly the oranges that were rotten when the minute began, and the fresh neighbours they rot wait at the back for the next minute. Spreading in rings like this is what makes the minute count the minimum.

Try:
CALL
1 / 1

Code

The highlighted line runs next. The pill shows the live values in the running call.

Who rotted whom

Each orange hangs under the one that rotted it · hover to find its cell

not rotten yet being processed waiting in the queue done spreading depth = the minute it rotted

Queue

BFS uses a queue where recursion would use the call stack

Grid

Rows on the left, columns on top; the outer ring is off the grid

fresh (1) rotten (2), number = minute it rotted empty (0) in the queue being processed

wavefront which oranges rotted at each minute

Keyboard: ← → step, Space play or pause. Grids up to 8×8. Paste LeetCode style ([[2,1],[1,0]]) or rows like 21, 10. Why BFS: a fresh orange rots at the minute equal to its shortest distance from any rotten orange, and BFS visits cells in exactly that order.