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.
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?

本题就是一个状态更新问题,更新条件如下:

  1. 原本为1的cell,若周围只有0或1个cell为1的话,则变为0;
  2. 原本为1的cell,若周围有2或3个cell为1的话,仍未1保持不变;
  3. 原本为1的cell,若周围有超过3个cell为1的话,则变为0;
  4. 原本为0的cell,若周围有3个cell为1的话,则变为1;

题目的难度在于其要求in-place的方式完成状态更新,讲道理这道题目我没有找到合适的不用额外空间的思路。看了discuss之后感觉豁然开朗,原来还可以这样====
思路是这样的,使用2bit位来表示细胞状态,即使用1和2来表示细胞死亡,0和3表示细胞存活(其他排列组合方式也是可以的)。最后再使用一个循环将其变成0和1。代码如下所示:

    public void gameOfLife1(int[][] board) {
        int m = board.length, n = board[0].length;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                int countLive = 0;
                for (int p = Math.max(i-1,0); p < Math.min(i+2,m); p++) {
                    for (int q = Math.max(j-1,0); q < Math.min(j+2,n); q++) {
                        if (board[p][q]==2||board[p][q]==1) countLive++;//count status 0 is live
                    }
                }
                countLive -= board[i][j];
                if (board[i][j] == 0 && countLive == 3) board[i][j] = 3; //status 0 is dead,next status is live
                if (board[i][j] == 1 && (countLive < 2 || countLive > 3)) board[i][j] = 2; //status 0 is live,next status is dead

            }
        }
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                board[i][j] %= 2;
            }
        }
    }

下面这种改进方法可以有效的提高执行效率,击败90%的用户:

    public void gameOfLife(int[][] board) {
        //int[][] tmp = new int [board.length][board[0].length];
        for(int i=0; i<board.length; i++){
            for(int j=0; j<board[0].length; j++){
                int count = count(board, i, j);
                if(board[i][j] == 1) {
                    if (count == 2 || count == 3)
                        board[i][j] = 3;
                }else{
                    if(count == 3)
                        board[i][j] = 2;
                }
            }
        }

        for(int i=0; i<board.length; i++)
            for(int j=0; j<board[0].length; j++){
                board[i][j] >>= 1;
            }
    }

    public int count(int[][] board, int row, int col){
        int sum = 0;
        for(int i = row-1; i<=row+1; i++)
            for(int j = col-1; j<=col+1; j++)
                if(i>=0 && i<board.length && j>=0 && j<board[0].length)
                    sum += board[i][j] & 1;
        sum -= board[row][col] & 1;
        return sum;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值