Plant Poison Spread

Asked at Apple· Screen

Reported by the problem's creator, not verified

Import another question

A field of plants is an m x n grid. 0 is a healthy plant and 1 is a poisoned one. The neighbors of a plant are the up to 8 plants around it, horizontally, vertically and diagonally; plants on an edge or in a corner have fewer.

Each day, every healthy plant with at least k poisoned neighbors becomes poisoned. All plants change at the same moment: what happens on a day depends only on the field as it was at the start of that day. A plant that turns poisoned on a day counts as a poisoned neighbor from the next day on. Poisoned plants stay poisoned.

Examples

  1. Example 1

    Input: grid = [[0,1,1,0],[0,0,0,0],[0,0,0,0]], k = 2

    Output: 3

    Explanation: On day 1 the two cells under the poisoned pair each touch two poisoned plants, so they turn. On day 2 the top corners, the left and right cells of the middle row and the two bottom-middle cells each touch two poisoned plants. On day 3 the bottom corners touch three. After that nothing changes, so the answer is 3.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.