The board is made up of an m x n grid of cells, where each cell has an initial state: live (represented by a 1) or dead (represented by a 0). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules: 1. Any live cell with fewer than two live neighbors dies. 2. Any live cell with two or three live neighbors lives on. 3. Any live cell with more than three live neighbors dies. 4. Any dead cell with exactly three live neighbors becomes a live cell. The next state is created by applying the above rules simultaneously to every cell in the current state. Given the current state of the board, update the board to reflect its 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]]
Explanation: All cells update at once. (0,1) and (2,0) each have only one live neighbour, so they die. (1,0) and (3,1) are dead but have exactly three live neighbours, so they come alive. (1,2), (2,1) and (2,2) have two or three live neighbours, so they survive.
Example 2
Input: board = [[1,1],[1,0]]
Output: [[1,1],[1,1]]
Explanation: Each live cell has two live neighbours, so it survives. The dead cell (1,1) has exactly three live neighbours, so it becomes alive.
Example 3
Input: board = [[0,0],[0,0]]
Output: [[0,0],[0,0]]
Explanation: There are no live cells, so no dead cell can have three live neighbours, and the board stays empty.
Because all cells update simultaneously, we must read the old state while writing the new one. Encode the next state in the second bit (value 2) while the current state stays in bit 1. After scanning, shift every cell right by one to reveal the next generation.
Run checks all cases above. Submit evaluates all test cases.