2024-07-11
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
According to Baidu Encyclopedia, the Game of Life, or simply Life for short, is a cellular automaton invented by British mathematician John Horton Conway in 1970.
Given a panel containing m × n grids, each grid can be regarded as a cell. Each cell has an initial state: 1 for live cells, or 0 for dead cells. Each cell and its eight adjacent cells (horizontally, vertically, and diagonally) follow the following four survival laws:
If the number of living cells in the eight positions around a living cell is less than two, the living cell at that position dies;
If there are two or three living cells in the eight positions around a living cell, the living cell at that position is still alive;
If there are more than three living cells in the eight locations around a living cell, the living cell at that location dies;
If there are exactly three live cells around a dead cell, the dead cell at that location will be resurrected;
The next state is formed by applying the above rules to every cell in the current state simultaneously, where cell birth and death occur simultaneously. Given the current state of the mxn grid panel board, return the next state.
Example 1:
Input: board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]]
Output: [[0,0,0],[1,0,1],[0,1,1],[0,1,0]]
Example 2:
Input: board = [[1,1],[1,0]]
Output: [[1,1],[1,1]]
hint:
m == board.length
n == board[i].length
1