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. The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously.
Example:
Input: [ [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] ]
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?
题目大意:
计算下一时刻的细胞状态,当一个活细胞周围有2到3个活细胞的时候,该细胞才能继续存活,否则死亡,当一个死细胞周围正好有3个活细胞的时候,该细胞存活。
解法:
使用了一个countLive来记录一个细胞周围所有的活细胞的个数。题目要求是在数组原地上进行更改,但是如果更改现有的细胞状态,就会丢失上一时刻的细胞状态。所以我这种解法是o(m*n)的时间和空间复杂度。
java:
class Solution { int dir[][]={{0,1},{0,-1},{1,0},{-1,0},{1,1},{-1,-1},{1,-1},{-1,1}}; public void gameOfLife(int[][] board) { int m=board.length,n=board[0].length; int[][] countLive=new int[m][n]; int[][] countDead=new int[m][n]; for (int i=0;i<m;i++){ for (int j=0;j<n;j++){ for (int p=0;p<8;p++){ int nextx=i+dir[p][0]; int nexty=j+dir[p][1]; if (nextx<0||nexty<0||nextx>=m||nexty>=n) continue; countLive[i][j]+=board[nextx][nexty]; } } } for (int i=0;i<m;i++){ for (int j=0;j<n;j++){ if (board[i][j]==1){ if (countLive[i][j]<2||countLive[i][j]>3) board[i][j]=0; }else{ if (countLive[i][j]==3) board[i][j]=1; } } } } }
看了一下网上的解法,这种解法只使用了O(1)的空间复杂度,采用两位数字代表了四种状态,第一位数字代表下一时刻的状态,第二位数字代表上一时刻的状态:
[2nd bit, 1st bit] = [next state, current state] - 00 dead (next) <- dead (current) - 01 dead (next) <- live (current) - 10 live (next) <- dead (current) - 11 live (next) <- live (current)
得到当前状态,只需要board[i][j] & 1
得到下一时刻状态,需要board[i][j] >> 1
class Solution { private int countLives(int[][] board,int m,int n,int x,int y){ int lives=0; for (int i=Math.max(0,x-1);i<=Math.min(x+1,m-1);i++){ for (int j=Math.max(0,y-1);j<=Math.min(y+1,n-1);j++){ lives+=board[i][j]&1; } } lives-=board[x][y]&1; return lives; } public void gameOfLife(int[][] board) { if (board.length==0||board[0].length==0) return; int m=board.length,n=board[0].length; for (int i=0;i<m;i++){ for (int j=0;j<n;j++){ int lives=countLives(board,m,n,i,j); if (board[i][j]==1&&lives>=2&&lives<=3) board[i][j]=3; if (board[i][j]==0&&lives==3) board[i][j]=2; } } for (int i=0;i<m;i++){ for (int j=0;j<n;j++){ board[i][j]>>=1; } } } }