This question evaluates competency in modeling and analyzing discrete-time grid-based state propagation with threshold-based infection rules and boundary conditions.
You are given an n × m grid representing people in a city.
1
) or
healthy
(
0
).
K infected neighbors
.
Return the minimum number of time steps until all cells are infected.
0
.
-1
.
grid
: an
n × m
matrix of
0/1
K
: an integer threshold (
0 ≤ K ≤ 4
)
-1
if impossible.
K = 0
, then all healthy cells become infected after
1
step (unless already all infected).
(Assume 1 ≤ n, m ≤ 200 and aim for an efficient solution.)