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

二、我的思路

暴力解…就算暴力解,代码写的也不够漂亮,另外还debug了好久啊啊啊

新开一个m*n空间存下一轮的棋盘。遍历每个点,先统计它周围活着的人数。再根据该点的情况决定新棋盘的值。

class Solution {
    public void gameOfLife(int[][] board) {
        int m = board.length;
        int n = board[0].length;
        
        int[][] newBoard = new int[m][n];
        for(int i = 0; i < m; i ++){
            for(int j = 0; j < n; j ++){
                int alive = 0;
                for(int k = -1; k <= 1; k += 1){
                    for(int p = -1; p <= 1; p += 1){
                        if(i + k >= 0 && i + k < m && j + p >= 0 && j + p < n){
                            if(board[i + k][j + p] == 1){
                                alive ++;
                            }
                        }
                    }
                }
                
                if(board[i][j] == 1){
                    alive --;
                    if(alive < 2 || alive > 3){
                        newBoard[i][j] = 0;
                    }
                    else{
                        newBoard[i][j] = 1;
                    }
                }
                else{
                    if(alive == 3){
                        newBoard[i][j] = 1;
                    }
                    else{
                        newBoard[i][j] = 0;
                    }
                }
            }
        }
        
        for(int i = 0; i < m; i ++){
            for(int j = 0; j < n; j ++){
                 board[i][j] = newBoard[i][j];
            }
        }
        
        
    }
}
猜想能用DP...但看了discuss并没有。。


Follow up1: 如果要in place的话,只要能保持原有的0和1的数据,并且增加下一轮是否改变原值的信息即可。所以我选用了2表示1下一轮要变0,3表示0下一轮要变1.最后处理完棋盘后,把棋盘每个格子对2取余数即可。

Follow up2没读懂…


三、淫奇技巧

思路差不多,除了记录下一轮的方法有了左移右移。但是人家代码比我的好多了,各种情况条件整合的特别好。。。


详情参照:http://www.jianshu.com/p/27024d85b5da

class Solution {
public:
    void gameOfLife(vector<vector<int>>& board) {
        int row(board.size()), column(board[0].size());
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                int count = 0;
                for (int I = max(i - 1, 0); I < min(i + 2, row); I++)
                    for (int J = max(j - 1, 0); J < min(j + 2, column); J++)
                        count += board[I][J] & 1;
                if (count == 3 || (count == 2 && board[i][j]))
                    board[i][j] |= 2;
            }
        }
        for (int i = 0; i < row; i++)
            for (int j = 0; j < column; j++)
                board[i][j] >>= 1;
    }
};

作者:Terence_F
链接:http://www.jianshu.com/p/27024d85b5da
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

四、知识点



五、问题

如果把我的代码的最后赋值段改成board = newBoard; 结果是错误的。为什么呢?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值