[LeetCode]289. Game of Life

遍历,状态的转换

Posted by JinFei on February 18, 2020

题目描述

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. The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously.

Example1:

  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]
    ]

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为活细胞,0为死细胞,对于每个位置都满足如下的条件:
    1. 如果活细胞周围八个位置的活细胞数少于两个,则该位置活细胞死亡
    2. 如果活细胞周围八个位置有两个或三个活细胞,则该位置活细胞仍然存活
    3. 如果活细胞周围八个位置有超过三个活细胞,则该位置活细胞死亡
    4. 如果死细胞周围正好有三个活细胞,则该位置死细胞复活
  • 我们遍历board[i][j],遍历其相邻的位置,看是否满足上面三个 是要被置死还是死而复生的
  • 可以总结,活细胞 少于两个,超过三个,都会死亡 -》 将其置为一个大于0的数
  • 如果有三个活细胞,则死细胞复活 -》 置为 -1
class Solution {
public:
    void gameOfLife(vector<vector<int>>& board) {
        int row = board.size();
        int col = board[0].size();
        for(int i = 0; i < row; i++){
            for(int j = 0; j < col; j++){
                auto count = countNumbers(i, j, board);
                
                if(board[i][j] == 1){
                    if(count < 2 || count > 3){
                        board[i][j] = 2;    // will be killed
                    }
                }else{
                    if(count == 3){
                        board[i][j] = -1;   // will alive
                    }
                }
                
            }
        }
        for(int i = 0; i < row; i++){
            for(int j = 0; j < col; j++){
                if(board[i][j] == 2){
                    board[i][j] = 0;
                }
                if(board[i][j] == -1){
                    board[i][j] = 1;
                }
            }
        }
    }
private:
    int countNumbers(int r, int c, vector<vector<int>>& board){
        int count = 0;
        int row = board.size();
        int col = board[0].size();
        // left && right 两个位置
        if(c > 0 && board[r][c - 1] > 0) count++;
        if(c < col - 1 && board[r][c + 1] > 0) count++;
        
        // top  三个位置
        if(r > 0){
            if(board[r - 1][c] > 0) count++;
            if(c > 0 && board[r - 1][c - 1] > 0) count++;
            if(c < col - 1 && board[r - 1][c + 1] > 0) count++;
        }
        
        // bottom   三个位置
        if(r < row - 1){
            if(board[r + 1][c] > 0) count++;
            if(c > 0 && board[r + 1][c - 1] > 0) count++;
            if(c < col - 1 && board[r + 1][c + 1] > 0) count++;
        }
        return count;
        
    }
};

官方题解

  • 根据数组的细胞状态计算新一轮的细胞状态,这里会用到能同时代表过去状态和现在状态的复合状态。
  • 具体的计算规则如下所示:
  • 规则 1:如果活细胞周围八个位置的活细胞数少于两个,则该位置活细胞死亡。这时候,将细胞值改为 -1,代表这个细胞过去是活的现在死了;
  • 规则 2:如果活细胞周围八个位置有两个或三个活细胞,则该位置活细胞仍然存活。这时候不改变细胞的值,仍为 1;
  • 规则 3:如果活细胞周围八个位置有超过三个活细胞,则该位置活细胞死亡。这时候,将细胞的值改为 -1,代表这个细胞过去是活的现在死了。可以看到,因为规则 1 和规则 3 下细胞的起始终止状态是一致的,因此它们的复合状态也一致;
  • 规则 4:如果死细胞周围正好有三个活细胞,则该位置死细胞复活。这时候,将细胞的值改为 2,代表这个细胞过去是死的现在活了。
  • 根据新的规则更新数组;
  • 现在复合状态隐含了过去细胞的状态,所以我们可以在不复制数组的情况下完成原地更新;
  • 对于最终的输出,需要将 board 转成 0,1 的形式。因此这时候需要再遍历一次数组,将复合状态为 2 的细胞的值改为 1,复合状态为 -1 的细胞的值改为0。
class Solution {
public:
    void gameOfLife(vector<vector<int>>& board) {
        int row = board.size();
        int col = board[0].size();
        for(int i = 0; i < row; i++){
            for(int j = 0; j < col; j++){
                int cnt = countNum(board, i, j);
                if(board[i][j] == 1){
                    if(cnt < 2 || cnt > 3){
                        board[i][j] = 2;
                    }
                }else{
                    if(cnt == 3){
                        board[i][j] = -1;
                    }
                }
            }
        }
        for(int i = 0; i < row; i++){
            for(int j = 0; j < col; j++){
                if(board[i][j] == 2){
                    board[i][j] = 0;
                }else if(board[i][j] == -1){
                    board[i][j] = 1;
                }
            }
        }
    }
    int countNum(vector<vector<int>>& board, int r, int c){
        int row = board.size();
        int col = board[0].size();
        int cnt = 0;
        // left && right
        if(c > 0 && board[r][c - 1] > 0){
            cnt++;
        }
        if(c < col - 1 && board[r][c + 1] > 0){
            cnt++;
        }
        // top
        if(r - 1 >= 0){
            if(c - 1 >= 0 && board[r - 1][c - 1] > 0){
                cnt++;
            }
            if(board[r - 1][c] > 0){
                cnt++;
            }
            if(c + 1 < col && board[r - 1][c + 1] > 0){
                cnt++;
            }
        }

        // down
        if(r + 1 < row){
            if(c - 1 >= 0 && board[r + 1][c - 1] > 0){
                cnt++;
            }
            if(board[r + 1][c] > 0){
                cnt++;
            }
            if(c + 1 < col && board[r + 1][c + 1]){
                cnt++;
            }
        }
        return cnt;
    }
};