← 返回 openai 的题目列表Plant Infection by Neighbor Count
类型:online_judge
Problem: Plant Infection by Neighbor Count
You are given an m x n garden grid grid, where:
0 represents empty land;
1 represents a healthy plant;
2 represents an already infected plant.
You are also given an integer k. Infection spreads synchronously by minute:
At the end of each minute, a healthy plant becomes infected if at least k of its four-directional neighbors are already infected;
Plants infected during the current minute cannot affect other plants until the next minute;
Empty cells cannot be infected and do not spread infection.
Return the minimum number of minutes required to infect all healthy plants. If it is impossible, return -1.
Input Format
m n k
grid[0][0] grid[0][1] ... grid[0][n-1]
...
grid[m-1][0] ... grid[m-1][n-1]
Output Format
minimum minutes
Constraints
1 <= m, n <= 1000
1 <= k <= 4
grid[i][j] in {0, 1, 2}
Example
Input:
3 3 1
2 1 1
1 1 0
0 1 1
Output:
4
Example
Input
3 3 1
2 1 1
1 1 0
0 1 1
Output
4