Infection Spread Simulation
Asked at OpenAI
Reported by the problem's creator, not verified
Simulate an infection spreading on a rectangular grid, day by day. The grid passed in contains only 0 (healthy) and 1 (infected); its 1 cells are infected on day 0.
Neighbors of a cell are the up to 8 cells touching it horizontally, vertically or diagonally. All cells change at the same moment: every decision on a day uses the grid as it stood at the end of the previous day.
simulate(grid, infect_threshold, days) returns the grid after days days.
Examples
Example 1
Input: grid =
[[0,0,0],[0,1,0],[0,0,0]], infect_threshold =1, days =2Output:
[[1,1,1],[1,1,1],[1,1,1]]Explanation: With threshold 1, every healthy cell touching the infected center, including diagonally, becomes infected on day 1. Day 2 changes nothing because every cell is already infected.