According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970."
Given a board with m by n cells, each cell has an initial state live (1) or dead (0). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):
- Any live cell with fewer than two live neighbors dies, as if caused by under-population.
- Any live cell with two or three live neighbors lives on to the next generation.
- Any live cell with more than three live neighbors dies, as if by over-population..
- Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
Write a function to compute the next state (after one update) of the board given its current state.
Follow up:
- Could you solve it in-place? Remember that the board needs to be updated at the same time: You cannot update some cells first and then use their updated values to update other cells.
- In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches the border of the array. How would you address these problems?
Solution:
In order to do in-place, we set four states: 0, 1, 2, 3.
The states means [next state, current state] in binary representation.
For example,
0: 00 means current state is 0 and next state is 0.
1: 01 means current state is 1 and next state is 0.
2: 10 means current state is 0 and next state is 1.
3: 11 means current state is 1 and next state is 1.
In each round, we only have 0 and 1 in the grid, which means by default the next state is always 0.
For each cell, we can count the number of other living cells around it.
If the cell is a live cell, and it's surrounded by 2 or 3 live cells, it can live in the next round.
We set its value to 3.
If a cell is a dead cell, and it's surrounded by 3 live cells, it can live in the next round.
We set its value to 2.
In all other cases, the cell will dead.
Since the initial value of the next state is 0, we do not need to do anything.
After counting neighbors of all cells, we update the entire whole states in-place.
Simply, go through all cells and shift a bit to the right to discard the current states.
Again, after shifting the value of the new next states are initialized to 0.
Code:
public class Solution { public void gameOfLife(int[][] board) { if (board == null || board.length == 0) { return; } if (board[0] == null || board[0].length == 0) { return; } int m = board.length; int n = board[0].length; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { int live = countLive(board, m, n, i, j); if (board[i][j] == 1 && (live == 2 || live == 3)) { board[i][j] = 3; } if (board[i][j] == 0 && live == 3) { board[i][j] = 2; } } } for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { board[i][j] >>= 1; } } } public int countLive(int[][] board, int m, int n, int x, int y) { int live = 0; for (int i = Math.max(x - 1, 0); i <= Math.min(x + 1, m - 1); i++) { for (int j = Math.max(y - 1, 0); j <= Math.min(y + 1, n - 1); j++) { live += (board[i][j] & 1); } } live -= (board[x][y] & 1); return live; } }