LeetCode OJ 289. Game of Life
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?
【题目分析】
题目中一个细胞的生死是由他的邻居细胞中活细胞的数目确定的,他的邻居细胞位于上,下,左,右,左上,右上,左下,右下。规则如下:
1. 一个活细胞的邻居中如果活着的细胞少于两个,那么该细胞将会死亡;
2. 一个活细胞的邻居中如果活着的细胞等于两个或三个,那么该细胞将会存活;
3. 一个活细胞的邻居中如果活着的细胞多于三个,那么该细胞将会死亡;
4. 一个死细胞的邻居中如果活着的细胞等于三个,那么该细胞将会复活;
用一个0,1二维数组表示细胞的相对位置,0表示该位置的细胞死亡,1表示该位置细胞活着。更新细胞的状态,就地完成,不适用额外的存储空间。
【思路】
1. 该问题的难点在于就地完成,不使用额外的存储空间。在本题目中一个细胞状态变化后不能马上修改数组的内容,否则的话他的邻居细胞在统计他活着的邻居细胞时候数目将会是不正确的。我们必须把所有细胞的状态变化情况求出来,才能统一对细胞的状态进行改变。如何实现呢?
细胞的状态变化有两种情况1->0和0->1,我们设置两个标记,如果是1->0,我们把该位置赋值为-1,如果是0->1,我们把该细胞位置赋值为2。这样的话我们既保存了细胞的原始状态信息,也保存了细胞的变化情况。遍历每一个细胞,我们得到了所有细胞的变化情况,然后再次遍历数组,把所有-1变为0,所有2变为1即可。
2. 一个巧妙的思路如下:
【java代码1】
1 public class Solution { 2 public void gameOfLife(int[][] board) { 3 if(board == null || board.length == 0 || board[0].length == 0) return; 4 int row = board.length; 5 int col = board[0].length; 6 7 for(int i = 0; i < row; i++){ //第一次遍历,求出每一个细胞的状态变化情况 8 for(int j = 0; j < col; j++){ 9 int num = neightbors(board, i, j); 10 if(board[i][j] == 1 && (num < 2 || num > 3)) 11 board[i][j] = -1; 12 else if(board[i][j] == 0 && num == 3) 13 board[i][j] = 2; 14 else ; 15 } 16 } 17 18 for(int i = 0; i < row; i++){ //第二次遍历修改细胞的状态 19 for(int j = 0; j < col; j++){ 20 if(board[i][j] == -1) 21 board[i][j] = 0; 22 else if(board[i][j] == 2) 23 board[i][j] = 1; 24 else ; 25 } 26 } 27 } 28 29 public int neightbors(int[][] board, int i, int j){ //求某个细胞的活着的邻居数目 30 int left = Math.max(0, j-1); 31 int right = Math.min(j+1, board[0].length-1); 32 int top = Math.max(i-1, 0); 33 int button = Math.min(i+1, board.length-1); 34 35 int sum = 0; 36 for(int k = top; k <= button; k++){ 37 for(int t = left; t <= right; t++){ 38 if(board[k][t] == 1 || board[k][t] == -1) sum ++; 39 } 40 } 41 return board[i][j] == 0? sum : sum - 1; 42 } 43 }
【java代码2】
1 public void gameOfLife(int[][] board) { 2 if(board == null || board.length == 0) return; 3 int m = board.length, n = board[0].length; 4 5 for(int i = 0; i < m; i++) { 6 for(int j = 0; j < n; j++) { 7 int lives = liveNeighbors(board, m, n, i, j); 8 9 // In the beginning, every 2nd bit is 0; 10 // So we only need to care about when the 2nd bit will become 1. 11 if(board[i][j] == 1 && lives >= 2 && lives <= 3) { 12 board[i][j] = 3; // Make the 2nd bit 1: 01 ---> 11 13 } 14 if(board[i][j] == 0 && lives == 3) { 15 board[i][j] = 2; // Make the 2nd bit 1: 00 ---> 10 16 } 17 } 18 } 19 20 for(int i = 0; i < m; i++) { 21 for(int j = 0; j < n; j++) { 22 board[i][j] >>= 1; // Get the 2nd state. 23 } 24 } 25 } 26 27 public int liveNeighbors(int[][] board, int m, int n, int i, int j) { 28 int lives = 0; 29 for(int x = Math.max(i - 1, 0); x <= Math.min(i + 1, m - 1); x++) { 30 for(int y = Math.max(j - 1, 0); y <= Math.min(j + 1, n - 1); y++) { 31 lives += board[x][y] & 1; 32 } 33 } 34 lives -= board[i][j] & 1; 35 return lives; 36 }