Loading...
Loading...
Loading...
A server room is a grid. 0 is an empty slot, 1 is a healthy computer and 2 is an infected computer. Each minute, every healthy computer that shares a side with an infected one becomes infected. Print the number of minutes until no healthy computer is left, or -1 if some healthy computer can never be infected. Print 0 if there is none to begin with.
Example 1
Input
3 3 211 110 011
Output
4
Example 2
Input
3 2 21 01 10
Output
-1
Try the problem first. Open one hint at a time.
Start BFS from all infected cells at the same time.
The answer is the largest distance reached; check that no healthy cell is left.
Write your Python code, run it on the examples and submit it to hidden tests.
Solve in the editor