[Array]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?  

  思路:对于每一个cell,计算其周围cell的存活数量,然后根据上述四条规则来决定当前cell的存活或者死亡,在计算的过程中会遇到这样一个问题:当更新一个cell的状态(存活或死亡)时,更新的值会覆盖掉原来的值,导致当更新该cell周围的cell时会受到影响,因此我们需要保存更新前与更新后cell的值。在这里解决的办法是利用递归,在当前cell中的状态进行保存,当前层首先递归进入下一层进行计算(进入到最深层时进行最深层的cell赋值返回),然后返回当前层,再对当前层的cell状态进行赋值。

  

class Solution {
private:
    int x[3]={-1,1,0};
    int y[3]={-1,1,0};
    int countNeighbor(vector<vector<int> >& board,int i,int j){
        int count = 0;
        for(int movX=0;movX<3;++movX){
            for(int movY=0;movY<3;++movY){
                if(x[movX]==0&&y[movY]==0)
                    continue;
                int nowX=j+x[movX];
                int nowY=i+y[movY];
                if(nowX>=0&&nowX<board[0].size()&&nowY>=0&&nowY<board.size()){
                    if(board[nowY][nowX]==1)                    
                        count++;
                }
            }
        }
        return count;
        
    }
    void recursive(vector<vector<int> >& board,int i,int j){
        if(i==board.size())
            return;
        int neighbor =  countNeighbor(board,i,j);
        int status=board[i][j];
        if(board[i][j]==1){
            if(neighbor<2||neighbor>3)
                status=0;
            else
                status=1;
        }
        else{
            if(neighbor==3)
                status=1;
        }
        int ori_i=i;
        int ori_j=j++;
        if(j==board[0].size()){
            ++i; 
            j=0;
        }
        recursive(board,i,j);
        board[ori_i][ori_j]=status;    
    } 
public:
    void gameOfLife(vector<vector<int>>& board) {
        if(board.size()==0||board[0].size()==0)
                return;
        recursive(board,0,0);
    }
};

 

posted @ 2016-08-11 13:30  U_F_O  阅读(310)  评论(0编辑  收藏  举报