Thursday, October 5, 2017

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):
  1. Any live cell with fewer than two live neighbors dies, as if caused by under-population.
  2. Any live cell with two or three live neighbors lives on to the next generation.
  3. Any live cell with more than three live neighbors dies, as if by over-population..
  4. 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
  1. 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.
  2. 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:
The solution is straightforward. For each cell, we check the living cell count of all 8 neighbors, and set corresponding value based on 4 cases listed in the question. 
The difficulty is to do it in place:
There are only 4 cases when for each cell, 
Living cell remains alive 1=>1
Living cell died 1=>0
Dead cell remains dead: 0=>0;
Dead cell becomes a live cell 0=>1
If the cell remains its original status, we can keep its value, because it won't alter the counts for its neighboring cells. 
But if the cell flips, we need to indicate its original status and mark it is flipped. 
What I did is for 1=>0, I set it to 2, and for 0=>1, I set it to -1, so when I count all living cells, I check if the value is great than 0 or not. 
After one round of check, we need to update cell values for those cells that are flipped. 

 public void gameOfLife(int[][] board) {  
     if(board==null || board.length==0 || board[0].length==0) return;  
     for(int i=0;i<board.length;i++){  
       for(int j=0;j<board[0].length;j++){  
         int count=getLiveNeighbors(board,i,j);  
         if(board[i][j]==1 && (count<2 || count>3)) board[i][j]=2;  
         else if(board[i][j]==0 && count==3) board[i][j]=-1;  
       }  
     }  
     for(int i=0;i<board.length;i++){  
       for(int j=0;j<board[0].length;j++){  
         if(board[i][j]==2) board[i][j]=0;  
         else if(board[i][j]==-1) board[i][j]=1;  
       }  
     }      
   }  
   public int getLiveNeighbors(int[][] board, int i, int j){  
     int count=0;  
     int lowI=Math.max(0,i-1);  
     int hiI=Math.min(board.length-1,i+1);  
     int lowJ=Math.max(0,j-1);  
     int hiJ=Math.min(board[0].length-1,j+1);  
     for(int m=lowI;m<=hiI;m++){  
       for(int n=lowJ;n<=hiJ;n++){  
         if((m!=i || n!=j) && board[m][n]>0) count++;  
       }  
     }  
     return count;  
   }  

No comments:

Post a Comment