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?
题意:

生命游戏。给出一个m*n的二维数组,1代码活节点,0代表死节点。每个格子都有八个邻居。生命游戏遵循以下四条规则:

1、一个活的格子若只有一个活着没有邻居,在下一秒将因寂寞而死亡;

2、一个活的格子若有两个或者三个邻居,在下一秒将继续活着;

3、一个活的格子若有四个或者四个以上的邻居,在下一秒将因拥挤而死亡;

4、一个死的格子若有三个邻居,在下一秒将活过来。


思路:

对二维数组中的每个元素轮训,找出每个元素周围八个格子活着格子的数目,并判断下一秒是活着还是死亡。将每个格子下一秒的状态保存在当前格子的第二位中,当完成所有元素下一秒状态之后,同时更新所有元素状态。


代码:

c++版:

class Solution {
public:
    void gameOfLife(vector<vector<int>>& board) {
        
        int m = board.size();
        int n = m ? board[0].size() : 0;
        
        if(m==0 || n==0){
            return;
        }
        
        for(int i=0; i<m; ++i){
            for(int j=0; j<n; ++j){
                int count = 0;
                
                for(int k=max(i-1, 0); k<min(i+2, m); ++k){
                    for(int g=max(j-1, 0); g<min(j+2, n); ++g){
                        count += board[k][g] & 1;
                    }
                }
                
                if(count==3 || (count-board[i][j])==3){
                    board[i][j] |= 2;
                }
            }
        }
        
        for(int i=0; i<m; ++i){
            for(int j=0; j<n; ++j){
                board[i][j]>>=1;
            }
        }
    }
};
java版:

public class Solution {
    public void gameOfLife(int[][] board) {
        
        int m = board.length;
        int n = board[0].length;
        
        for(int i=0; i<m; ++i){
            for(int j=0; j<n; ++j){
                int lives = liveNeighbors(board, m, n, i, j);
                
                if(board[i][j]==1 && lives>=2 && lives<=3){
                    board[i][j] = 3; 
                }
                
                if(board[i][j]==0 && lives==3){
                    board[i][j] = 2;
                }
            }
        }
        
        for(int i=0; i<m; ++i){
            for(int j=0; j<n; ++j){
                board[i][j] >>= 1;
            }
        }
    }
    
    public int liveNeighbors(int[][] board, int m, int n, int i, int j){
        int lives = 0;
        for(int k=Math.max(i-1, 0); k<Math.min(i+2, m); ++k){
            for(int g=Math.max(j-1, 0); g<Math.min(j+2, n); ++g){
                lives += board[k][g] & 1;
            }
        }
        
        lives -= board[i][j] & 1;
        return lives;
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值