← 返回 anthropic 的题目列表Infection Simulation in a Grid
类型:online_judge
Given an n*m grid where some cells may be infected. Each healthy cell becomes infected if it has N infected neighbors (up, down, left, right). The problem is to calculate the time taken for all cells to become infected. The initial state and the value of N will be provided.
Input
An integer n representing the number of rows in the grid.
An integer m representing the number of columns in the grid.
n strings representing the infection state, 'H' for healthy and 'I' for infected.
An integer N representing the number of infected neighbors needed for a cell to become infected.
Output
An integer t representing the time taken for all cells to become infected. Return -1 if not all cells can be infected.
Example
Example 1:
Input: n = 3, m = 3,
'IHH',
'HHH',
'HHI',
N = 2
Output: 3
Example
Input
3 3
'IHH','HHH','HHI'
2