leetcode题解-289. Game of Life

来源:互联网 发布:mac 自动更新 快捷键 编辑:程序博客网 时间:2024/06/10 15:59

题目:

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
原创粉丝点击