Fill the blank
Fill the blank so the queue starts with every spoiled item.
from collections import deque
shelf = [[2, 1, 1], [1, 1, 0], [0, 1, 2]]
q = deque(____)
print(list(q))Choose an answer, then open Show answer below to check it.
Show answer
Answer: A (r, c) for r, row in enumerate(shelf) for c, v in enumerate(row) if v == 2
Right: every spoiled item is a starting point at minute 0.
Seeding the queue with all sources at once is what turns BFS into nearest-source distances.
Why not the others?
- B [(0, 0)]. That only seeds the corner and misses the second spoiled item.
- C (r, c) for r, row in enumerate(shelf) for c, v in enumerate(row) if v == 1. Fresh items are the targets, not the sources.
Steps come from running the Python solution.
Text version
One source:
Grid (✕ = blocked): □□□ / □□✕ / ✕□□.
- Visit (0, 0), frontier 1. Start: 1 source cell at distance 0
- Visit (1, 0), (0, 1), frontier 2. Distance 1: 2 new cells
- Visit (1, 1), (0, 2), frontier 2. Distance 2: 2 new cells
- Visit (2, 1), frontier 1. Distance 3: 1 new cell
- Visit (2, 2), frontier 1. Distance 4: 1 new cell
Result: 4
Two sources:
Grid (✕ = blocked): □□✕□ / ✕□□□ / □□✕□.
- Visit (0, 0), (2, 3), frontier 2. Start: 2 source cells at distance 0
- Visit (0, 1), (1, 3), frontier 2. Distance 1: 2 new cells
- Visit (1, 1), (0, 3), (1, 2), frontier 3. Distance 2: 3 new cells
- Visit (2, 1), frontier 1. Distance 3: 1 new cell
- Visit (2, 0), frontier 1. Distance 4: 1 new cell
Result: 4