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.
Write a function to compute the next state (after one update) of the board given its current state.

Follow up:
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.
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?

这个题主要是思路,太神奇了!实现并不复杂。编解码法:
这个题空间复杂度为O(1)。所以只能在数组之中变化。
难点是如果一个元素状态变化后,假如从1变0,那么附近的元素就会将它看成是0。所以这个时候就应该将每一个元素之前的值记录下来,然后之后的状态也得记录下来。所以下面就是巧妙之处:
将每一个元素都分成4种状态:
0->0 这个状态记为0
1->1 这个状态记为1
0->1 这个状态记为2
1->0 这个状态记为3
所以这样前后状态都记下来了
当之前记录1的个数的时候就将1和3全部都记录下来就可以了。
最后遍历数组,将所有的2还原成1,3还原成0,即可得到正确答案!

下面是代码:
if-else语句最好用?:运算符,这样节省能使代码变得简洁很多
嵌套if语句也可以用拆开用&&运算符连接即可

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 state=board[i][j], count1=0;
            if(i>0)
                count1+=board[i-1][j]==1||board[i-1][j]==3?1:0;
            if(j>0&&i>0)
                count1+=board[i-1][j-1]==1||board[i-1][j-1]==3?1:0;
            if(j<n-1&&i>0)
                count1+=board[i-1][j+1]==1||board[i-1][j+1]==3?1:0;
            if(j>0)
                count1+=board[i][j-1]==1||board[i][j-1]==3?1:0;
            if(j<n-1)
                count1+=board[i][j+1]==1||board[i][j+1]==3?1:0;
            if(i<m-1)
                count1+=board[i+1][j]==1||board[i+1][j]==3?1:0;
            if(j>0&&i<m-1)
                count1+=board[i+1][j-1]==1||board[i+1][j-1]==3?1:0;
            if(j<n-1&&i<m-1)
                count1+=board[i+1][j+1]==1||board[i+1][j+1]==3?1:0;
            if(state==1){
                board[i][j]=count1<2||count1>3?3:1;
            }else{
                board[i][j]=count1==3?2:0;
            }
        }
    }
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            board[i][j]=(board[i][j]==2||board[i][j]==1)?1:0;
        }
    }
}

还有代码优化的方法:具体的请看连接优化方法

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值