According to 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."

The board is made up of an m x n grid of cells, where each cell has an initial state: live (represented by a 1) or dead (represented by a 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.
The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously. Given the current state of the m x n grid board, return the next state.

 

Example 1:


Input: board = [[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]]
Example 2:


Input: board = [[1,1],[1,0]]
Output: [[1,1],[1,1]]
 

Constraints:

m == board.length
n == board[i].length
1 <= m, n <= 25
board[i][j] is 0 or 1.
 

Follow up:

Could you solve it in-place? Remember that the board needs to be updated simultaneously: 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 upon the border of the array (i.e., live cells reach the border). How would you address these problems?

这题是一个二维数组的数据结构。我们需要遍历某个格子上周边的格子来确定它最终的值。需要注意的是,变更后的值不应该影响周边格子的计算,该计算是基于最开始的格子的快照。

不额外分配内存的情况下,我们可以利用int,毕竟它的值的范围很大。我们可以判断最低位的值为开始的状态,后一位为变更后的状态,如此来生成最后的数组。

最后,我们根据最低位后一位的值再来恢复原先的0和1即可。


class GameOfLife : public Solution {
public:
    void Exec() {
        
    }
    void gameOfLife(vector<vector<int>>& board) {
        vector<vector<int>> newBoard{board.size(), vector<int>(board[0].size(), 0)};
        for (int i = 0; i < board.size(); i++) {
            for (int j = 0; j < board[i].size(); j++) {
                int count = 0;
                for (int k = std::max(0, i - 1); k <= std::min(int(board.size()) - 1, i + 1); k++) {
                    for (int l = std::max(0, j - 1); l <= std::min(int(board[0].size() - 1), j + 1); l++) {
                        if (k == i && l == j) continue;
                        if ((board[k][l] & 0x01) == 1) {
                            count++;
                        }
                    }
                }
                if (board[i][j] == 1) {
                    board[i][j] |= (count < 2 || count > 3) ? 0 : 0x02;
                } else if (count == 3) {
                    board[i][j] |= 0x02;
                }
            }
        }
        for (auto &row : board) {
            for (auto &v : row) {
                if ((v & 0x02) != 0) {
                    v = 1;
                } else {
                    v = 0;
                }
            }
        }
    }
};
共 0 条回复
暂时没有人回复哦,赶紧抢沙发
发表新回复

作者

sryan
today is a good day