[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);
    }
};

 

转载于:https://www.cnblogs.com/GoFly/p/5760614.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值