[289] leetcode Game of Life (array, in-place algorithm)

Topic links: https://leetcode-cn.com/problems/game-of-life/

Title Description

According to Baidu Encyclopedia, Game of Life, referred to as life, is a British mathematician John Horton Conway cellular automata 1970 invention.

Given a, m × n grid panels, each grid can be seen as a cell. Each cell has an initial state live (1) that is live cells or dead (0) is the dead cells. Per cell to its eight neighboring positions (horizontal, vertical, diagonal) are the following four cell survival Law:

  1. If the number of viable cells of eight positions around fewer than two live cells, the location of a live cell death;
  2. If there are eight positions around the living cells of two or three living cells, the location of living cells were still alive;
  3. If there are eight positions around living cells over three living cells, the location of a live cell death;
  4. If exactly three dead cells around the living cells, dead cells revive the position;
    the current state, a write function to calculate the next state (after the last update) cells on the panel. The next state is formed by the above rules simultaneously applied to each cell in the current state, wherein the birth and death of cells occurs simultaneously.

Example:

输入: 
[
  [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]
]

Advanced:

  • You can use the in-place algorithm to solve this problem it? Please note that the panel all the grid needs to be updated at the same time: you can not update some of the first grid, and then use the value of their update and then update the other squares.
  • In this problem, we use a two-dimensional array to represent the panel. In principle, the panel is infinite, but living cells invaded cause problems when the panel border. How would you solve these problems?

Thinking

Similar problems zero matrix 73, using the two in-place algorithm to iterate.
(1) When the first pass, marking bit needs to be updated; live cells -> -1 dead cell marker, dead cells -> 1 labeled viable cells;
(2) the second pass, the mark - 1 is set to 0, 1, 2 is set for the flag

Complexity Analysis

  • Time complexity: O (mn)
  • Space complexity: O (1)

Code

/*
 * -1表示当前为1,下一次更新为0的细胞
 * 2表示当前为0,下一次更新为1的细胞
 */
class Solution {
public:
    void gameOfLife(vector<vector<int>>& board) {
        if(board.empty() || board[0].empty()) return;
        int rows = board.size(), cols = board[0].size();
        
        // 标记状态
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < cols; ++j) {
                int cnt = 0;
                // 统计活细胞数量
                for (int k = i-1; k <= i+1 ; ++k) {
                    for (int l = j-1; l <= j + 1 ; ++l) {
                        if(k<0 || l < 0|| k>=rows || l>=cols || (k==i && l ==j))
                            continue;
                        if(board[k][l] == 1 ||board[k][l] == -1)
                            cnt ++;
                    }
                }
                if (board[i][j] == 1){
                    if(cnt < 2 || cnt > 3)
                        board[i][j] =  -1;
                }
                else if(cnt == 3)
                    board[i][j] = 2;
            }
        }

        // 更新状态
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < cols; ++j) {
                if(board[i][j] == -1)
                    board[i][j] = 0;
                else if(board[i][j] ==2)
                    board[i][j] = 1;
            }
        }
    }
};

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/zjwreal/article/details/95322404