细胞生死问题 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."this

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

Follow upthree

  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?

解决:游戏

【题意】 ip

这道题是有名的康威生命游戏, 这是一种细胞自动机,每个位置有两种状态,1为活细胞,0为死细胞,对于每一个位置都知足以下的条件:ci

1. 若是活细胞周围八个位置的活细胞数少于两个,则该位置活细胞死亡;get

2. 若是活细胞周围八个位置有两个或三个活细胞,则该位置活细胞仍然存活;it

3. 若是活细胞周围八个位置有超过三个活细胞,则该位置活细胞死亡;

4. 若是死细胞周围正好有三个活细胞,则该位置死细胞复活;

题目要求咱们用置换方法in-place来解题,因此咱们就不能新建一个相同大小的数组,那么咱们只能更新原有数组,可是题目中要求全部的位置必须被同时更新,可是在循环程序中咱们仍是一个位置一个位置更新的,那么当一个位置更新了,这个位置成为其余位置的neighbor时,咱们怎么知道其未更新的状态呢,咱们能够使用状态机转换:

状态0(00): 死细胞转为死细胞

状态1(01): 活细胞转为活细胞

状态2(10): 活细胞转为死细胞

状态3(11): 死细胞转为活细胞

咱们将全部状态对2取余,那么状态0和2就变成死细胞,状态1和3就是活细胞,达成目的。

① 直接实现上面的解法。

 class Solution{ //0ms
    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 lives = 0;
                if (i > 0){//判断上边
                    lives += board[i - 1][j] == 1 || board[i - 1][j] == 2 ? 1 : 0;//状态1,2初始为活细胞
                }
                if (j > 0){//判断左边
                    lives += board[i][j - 1] == 1 || board[i][j - 1] == 2 ? 1 : 0;
                }
                if (i < m - 1){//判断下边
                    lives += board[i + 1][j] == 1 || board[i + 1][j] == 2 ? 1 : 0;
                }
                if (j < n - 1){//判断右边
                    lives += board[i][j + 1] == 1 || board[i][j + 1] == 2 ? 1 : 0;
                }
                if (i > 0 && j > 0){//判断左上角
                    lives += board[i - 1][j - 1] == 1 || board[i - 1][j - 1] == 2 ? 1 : 0;
                }
                if (i < m - 1 && j < n - 1){//判断右下角
                    lives += board[i + 1][j + 1] == 1 || board[i + 1][j + 1] == 2 ? 1 : 0;
                }
                if (i > 0 && j < n - 1){//判断右上角
                    lives += board[i - 1][j + 1] == 1 || board[i - 1][j + 1] == 2 ? 1 : 0;
                }
                if (i < m - 1 && j > 0){//判断左下角
                    lives += board[i + 1][j - 1] == 1 || board[i + 1][j - 1] == 2 ? 1 : 0;
                }
                if (board[i][j] == 0 && lives == 3){//死细胞转换为活细胞
                    board[i][j] = 3;
                }else if (board[i][j] == 1){//活细胞转为死细胞
                    if (lives < 2 || lives > 3) board[i][j] = 2;
                }
            }
        }
        //解码
        for (int i = 0;i < m;i ++){
            for (int j = 0;j < n;j ++){
                board[i][j] = board[i][j] % 2;
            }
        }
    }
}

② 使用位操做进行编码。

class Solution{ //1ms     public void gameOfLife(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 lives = 0;                 // 累加上下左右及四个角还有自身的值                 for(int y = Math.max(i - 1, 0); y <= Math.min(i + 1, m - 1); y++){                     for(int x = Math.max(j - 1, 0); x <= Math.min(j + 1, n - 1); x++){                         // 累加bit1的值                         lives += board[y][x] & 1;                     }                 }                 // 若是本身是活的,周边有两个活的,lives是3                 // 若是本身是死的,周边有三个活的,lives是3                 // 若是本身是活的,周边有三个活的,lives减本身是3                 if(lives == 3 || lives - board[i][j] == 3){                     board[i][j] |= 2;                 }             }         }         // 右移就是新的值         for(int i = 0; i < m; i++){             for(int j = 0; j < n; j++){                 board[i][j] >>>= 1;             }         }     } }

相关文章
相关标签/搜索