LeetCode-289: Game of Life

One, Title Description

Find a good from the comments simulator
given one of the m × n grid panels comprise, for each grid can be seen as a cell. Each cell having an initial state: 1 is the viable cells (live), that is, 0, or dead cells (dead). Per cell to its eight neighboring positions (horizontal, vertical, diagonal) are the following four cell survival Law:

  • If the number of viable cells of eight positions around fewer than two live cells, the location of a live cell death;
  • If there are eight positions around the living cells of two or three living cells, the location of living cells were still alive;
  • If there are eight positions around living cells over three living cells, the location of a live cell death;
  • If you happen to have three dead cells around the living cell, the position of the dead cells resurrection;

According to the current state, a write function to calculate the next state (after update) on all the cells of 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]
]
输出:
[
  [0,0,0],
  [1,0,1],
  [0,1,1],
  [0,1,0]
]

Second, problem-solving point

It should be nothing difficult, most notably, the updated value, you can not update only a part, then this small part that has been updated to update a large addition is not updated. So we think can save the current state

class Solution {
public:
    void gameOfLife(vector<vector<int>>& board) {
        auto m = board.size();
        auto n = board[0].size();
        if(!m || !n)    return;
        int i = 0, j = 0;
        auto tmp(board);	//保存当前图谱状态
        unsigned int alive = 0;
        for(i = 0; i < m; i++){
            for(j = 0; j < n; j++){
                if(i - 1 >= 0 && j - 1 >= 0 && tmp[i - 1][j - 1]) alive++;
                if(i - 1 >= 0 && tmp[i - 1][j])  alive++;
                if(i - 1 >= 0 && j + 1 < n && tmp[i - 1][j + 1])  alive++;
                if(j - 1 >= 0 && tmp[i][j - 1])  alive++;
                if(j + 1 < n  && tmp[i][j + 1])  alive++;
                if(i + 1 < m  && j - 1 >= 0  && tmp[i + 1][j - 1])  alive++;
                if(i + 1 < m  && tmp[i + 1][j])  alive++;
                if(i + 1 < m  && j + 1 < n && tmp[i + 1][j + 1])  alive++;

                if(alive < 2 && tmp[i][j])   board[i][j] = 0;
                if((alive == 2 || alive == 3) && board[i][j])   board[i][j] = 1;
                if(alive > 3 && tmp[i][j])   board[i][j] = 0;
                if(alive == 3 && !tmp[i][j])   board[i][j] = 1;

                alive = 0;
            }
        }
    }
};

Third, the operating results

Still the same puzzle, to optimize space, in-situ conversion but the gall space 100 % 100\% ???
Here Insert Picture Description

Published 30 original articles · won praise 3 · Views 825

Guess you like

Origin blog.csdn.net/weixin_44587168/article/details/105274525