leetcode 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):

Any live cell with fewer than two live neighbors dies, as if caused by under-population.
Any live cell with two or three live neighbors lives on to the next generation.
Any live cell with more than three live neighbors dies, as if by over-population…
Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.

Example:

Input:
[
[0,1,0],
[0,0,1],
[1,1,1],
[0,0,0]
]
Output:
[
[0,0,0],
[1,0,1],
[0,1,1],
[0,1,0]
]

给出一个数组,描述目前的life状态,然后给出几个生存规则,
现在是0时,如果它的一圈加起来只有不到2,下一状态还是0
现在是1时,如果它的一圈加起来是2或3,下一状态还是1
现在是1时,如果它的一圈加起来多于3,下一状态是0
现在是0时,如果它的一圈加起来是3,下一状态是1

总结一下:

下一状态初始化为0,所以不需要专门去设0,只需要考虑下一状态为1的情况

下一状态为1的情况:
不包含自身的话:
现在是0,周围一圈加起来是3
现在是1,周围一圈加起来是2或3

包含自身的话:
现在是0,3✖️3范围内的和是3
现在是1,3✖️3范围内的和是3或4

进一步总结:
下一状态为1的情况:
3✖️3范围内的和是3
3✖️3范围内的和 - 自身 = 3

第二个问题,直接更新board中的元素会对下一步运算产生影响
就需要重新定义一个一样大的数组来保存下一状态,但是题中函数没有返回值,所以最后还需要把保存的新状态再copy回原数组中,空间复杂度就是O(mn),时间也是O(mn)

为了达到O(1)空间复杂度,要有效利用bit位,因为状态只有0,1,所以仅需要1bit,可以用高一位的bit来保存下一状态,最后右移一位即得到结果

注意高位更新后,为了不对下一步计算产生影响,计算时用board[i][j] & 1来得到现在的状态

3✖️3窗口越界检测用max(0, i+1),min(max_row, i+1)来做,同理列

    public void gameOfLife(int[][] board) {
        if (board == null || board.length == 0 || board[0].length == 0) {
            return;
        }
        
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                int lives = 0;
                for (int m = Math.max(0, i-1); m <= Math.min(board.length-1, i+1); m++) {
                    for (int n = Math.max(0, j-1); n <= Math.min(board[0].length-1, j+1); n++) {
                        lives += board[m][n] & 1;
                    }
                }
                if (lives == 3 || (lives - (board[i][j] & 1) == 3)) {
                    board[i][j] |= 0x0002;
                }
            }
        }
        
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                board[i][j] >>= 1;
            }
        }
    }

或者不加自身,根据8个邻居的和来判断

    public void gameOfLife(int[][] board) {
        //1: die : <2, >3
        //1: live: 2~3
        //0: live: 3
        int rows = board.length;
        int cols = board[0].length;
        
        for(int i = 0; i < rows; i++) {
            for(int j = 0; j < cols; j++) {
                int sum = 0;
                for(int m = Math.max(i-1, 0); m <= Math.min(i+1, rows-1); m++) {
                    for(int n = Math.max(j-1, 0); n <= Math.min(j+1, cols-1); n++){
                        if(m == i && n == j) continue;
                        sum += board[m][n] & 0x0001;
                    }
                }
                if(sum == 3 || ((board[i][j] & 0x0001) == 1 && sum == 2)) board[i][j] |= 0x0002;
                
            }
        }
        
        for(int i = 0; i < rows; i++) {
            for(int j = 0; j < cols; j++) {
                board[i][j] >>= 1;
            }
        }
    }
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值