You are given an m x n grid representing a garden:
-
0
: empty cell
-
1
: healthy plant
-
2
: infected plant
The garden evolves day by day in the following order:
-
At the start of the day, you may optionally perform one intervention: burn exactly one entire row or one entire column. Every plant in that line is removed immediately. Burned cells become empty forever.
-
Each infected plant spreads the infection to its orthogonally adjacent healthy neighbors.
-
After the spread, check each infected plant. If an infected plant has ever had at least
K
orthogonally adjacent infected neighbors at the end of a day, it becomes marked for death.
-
A marked plant dies exactly
D
days after the first day it was marked, and the cell becomes empty.
Assume that once a plant is marked, its death timer does not reset. Dead or burned cells do not spread infection, cannot be infected again, and are not counted as infected neighbors.
Answer both parts:
-
Given the initial grid,
K
, and
D
, compute how many plants eventually die because of the overcrowding rule.
-
You may use the burn intervention at most once, on any chosen day. Burned plants are not counted as deaths. Design an algorithm to choose the best day and the best row or column to burn so that the number of eventual deaths is minimized.