Wednesday, December 16, 2015

289. Game of Life -M

Q
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.  
  3. 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?
A:
原 >> 后 ---更改后的代表
1  >> 0 ---   10
0  >> 1 ---   12
先按照上面的改。  最后再全部改过来就行了

class Solution {
public:
    void gameOfLife(vector<vector<int>>& board) {
        int m = board.size();
        if( m == 0)
            return ;
        int n = board[0].size();
        for(int i =0;i<m;++i)
        {
            for(int j = 0; j<n; ++j)
            {
                int count = helper(board, i,j);   // 1->0 (10),  0 ->1 (12)
                int &ref = board[i][j];
                if(ref == 1){
                    if(count<2 or count > 3)
                        ref = 10;
                }else{
                    if(count == 3)
                        ref = 12;
                }
            }
        }        
        // put all as 0/1
        for(int i =0;i<m;++i)
        {
            for(int j = 0; j<n; ++j)
            {
                int &ref = board[i][j];
                if(ref == 10)
                    ref = 0;
                else if (ref==12)
                    ref = 1;
            }
        }        
    }
private:
    int helper( vector<vector<int>>& board, int row, int col )
    {        
        int m = board.size();        
        int n = board[0].size();
        int count = 0;
        for(int i = max(0,row-1); i<=min(m-1, row+1); ++i )
        {
            for(int j = max(col-1, 0); j<= min(n-1, col+1); ++j)
            {
                if(i==row and j == col)
                    continue;
                if(board[i][j] ==1 or board[i][j] == 10)
                    count++;
            }
        }
        return count;
    }
};

Mistakes:

*********    Put on another List  to record the positions to be changed ********* 


public class Solution {
    public void gameOfLife(int[][] board) {
        int m = board.length;
        if(m==0)
            return;
        int n = board[0].length;
        List<int[]> liveList = new LinkedList();
        List<int[]> dieList = new LinkedList();
        for(int i =0;i<m;i++){
            for(int j =0;j<n; j++ ){
                int nNeibor = nNeighbor(board,i,j);
                if(board[i][j] == 1 && (nNeibor<2 || nNeibor > 3 )){
                    int[] tmp = {i,j};
                    dieList.add(tmp);
                }
                if(board[i][j] == 0 && nNeibor == 3){
                    int[] tmp = {i,j};
                    liveList.add(tmp);
                }
            }
        }
        for(int[] A : liveList){
            board[A[0]][A[1]] = 1;
        }
        for(int[] A : dieList){
            board[A[0]][A[1]] = 0;
        }
        
    }
    private int nNeighbor(int[][] board, int x ,int y){
        int m = board.length, n = board[0].length;
        int count = 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++ ){
                if( i ==x && j == y)
                    continue;
                if(board[i][j]==1 )
                    count++;
            }
        }
        return count;
    }
}







No comments:

Post a Comment