2016年10月23日星期日

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?
对于这道题,因为不同的时刻的状态不能混淆,因此需要另开一个board 来存未来状态

但是由于状态只有两个0或1,1个bit,因此可以用第二个bit来表示下一个状态。算完之后,每个数字向右移动一位就可以了

1. in-place

 public class Solution {  
   public void gameOfLife(int[][] board) {  
     int m = board.length;  
     if(m == 0) return ;  
     int n = board[0].length;  
     for(int i = 0; i < m; i++){  
       for(int j = 0; j < n; j++){  
         int countLives = countLives(board,i,j);  
         System.out.println(countLives);  
         if((board[i][j] & 1) == 0 && countLives == 3){  
           board[i][j] |= 2;  
         }else if ((board[i][j] & 1) == 1 && (countLives== 3 || countLives == 2)){  
           board[i][j] |= 2;  
         }  
       }  
     }  
     for(int i = 0; i < m; i++)  
       for(int j = 0; j < n; j++)  
         board[i][j]>>=1;  
   }  
   public int countLives(int[][]board,int i,int j){  
     int m = board.length;  
     int n = board[0].length;  
     int[] dir = {-1,0,1};  
     int count = 0;  
     for(int dr: dir){  
       for(int dc:dir){  
         if(dr==0&&dc==0|| i+dr<0||j+dc<0||i+dr==m||j+dc==n)continue;  
         count += (board[i+dr][j+dc]&1);  
       }  
     }  
     return count;  
   }  
 }  




2. board is infinite:只存live 的点,算neighbors:neighbors[I, J] += 1
如果neighbors[I, J] == 3,说明这个点周围有且只有三个live,一定活下去,如果neighbors[I, J] == 2 说明这个点周围只有2个live,这种情况I, J一定要在原来live的List才能活下去。

def gameOfLifeInfinite(self, live):
    neighbors = collections.Counter()
    for i, j in live:
        for I in (i-1, i, i+1):
            for J in (j-1, j, j+1):
                if I != i or J != j:
                    neighbors[I, J] += 1
    new_live = set()
    for ij in neighbors.keys():
        if neighbors[ij] == 3 or neighbors[ij] == 2 and ij in live:
            new_live.add(ij)
    return new_live

没有评论:

发表评论