This question evaluates understanding of grid-based graph traversal and propagation dynamics, testing competencies in breadth-first search concepts, simulation of state changes, and analysis of time and space complexity.
You are given an m x n grid representing a population during an outbreak:
0
= empty cell
1
= healthy person
2
= infected person
Every minute, each infected person spreads the disease to any healthy person in the four adjacent cells: up, down, left, and right.
Return the minimum number of minutes needed until no healthy person remains. If it is impossible to infect everyone, return -1.
Example:
[[2,1,1],[1,1,0],[0,1,1]]
4
Discuss the algorithm, its time complexity, and important edge cases.