1141. Seeds on a Torus
A garden grows on a wrap-around board where the top edge touches the bottom edge and the left edge touches the right edge. In board a cell is 1 (a seed, alive) or 0 (empty).
In each generation a dead cell becomes alive if exactly 2 of its 8 surrounding cells are alive, with row and column indices taken modulo the board size, and every live cell dies. All cells change at the same moment. Return a new grid with the state after k generations; for k = 0 return a copy of the board. Simulate one generation at a time, which costs O(k*m*n) time and O(m*n) space.
Example 1
- Input:
- board = [[0,0,0],[0,1,0],[0,0,0]], k = 1
- Output:
- [[0,0,0],[0,0,0],[0,0,0]]
- Explanation:
The only live cell dies, and every other cell sees just one live neighbour, so nothing is born and the board is empty.
Example 2
- Input:
- board = [[0,0,0,0,0],[0,0,0,0,0],[0,1,1,0,0],[0,0,0,0,0],[0,0,0,0,0]]k = 1
- Output:
- [[0,0,0,0,0],[0,1,1,0,0],[0,0,0,0,0],[0,1,1,0,0],[0,0,0,0,0]]
- Explanation:
Both seeds die. The four cells directly above and below the pair each touch both seeds, so exactly 2 neighbours are alive and they come alive.
Constraints
3 ≤ m, n ≤ 30
0 ≤ k ≤ 50
Each cell of board is 0 or 1
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(k*m*n)
- Space
- O(m*n)