LeetCode C++ 289. Game of Life【Array】中等

这篇博客介绍了如何使用C++实现康威的生命游戏,包括两种解法:一种使用辅助空间,另一种采用原地更新。在原地更新的解法中,通过利用int类型存储当前和下一个状态,减少了额外的空间消耗。同时,博客讨论了如何处理边界问题以及在有限的二维数组中模拟无限大的生命游戏面板。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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):

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

Example:

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?

题意:给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞。每个细胞都具有一个初始状态:1 即为活细胞, 0 即为死细胞。每个细胞与其八个相邻位置(水平,垂直,对角线)的细胞都遵循以下四条生存定律:

  • 如果活细胞周围八个位置的活细胞数少于两个,则该位置活细胞死亡;
  • 如果活细胞周围八个位置有两个或三个活细胞,则该位置活细胞仍然存活;
  • 如果活细胞周围八个位置有超过三个活细胞,则该位置活细胞死亡;
  • 如果死细胞周围正好有三个活细胞,则该位置死细胞复活;

根据当前状态,计算面板上所有细胞的下一个(一次更新后的)状态。下一个状态是通过将上述规则同时应用于当前状态下的每个细胞所形成的。


解法1 辅助空间

开一个 O ( n m ) O(nm) O(nm) 的二维数组,然后更新原 board

class Solution {
public:
    void gameOfLife(vector<vector<int>>& board) {
        int n = board.size(), m = board[0].size();
        vector<vector<int>> temp(n, vector<int>(m));
        int Moves[][2] = {0, 1, 0, -1, 1, 0, -1, 0, -1, -1, -1, 1, 1, -1, 1, 1};
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                int cells = 0;
                for (int k = 0; k < 8; ++k) {
                    int x = i + Moves[k][0], y = j + Moves[k][1];
                    if (x >= 0 && x < n && y >= 0 && y < m && board[x][y] == 1) ++cells;
                }
                if (board[i][j] == 1) { //活细胞
                    if (cells < 2) temp[i][j] = 0; //周围活细胞数<2
                    else if (cells <= 3) temp[i][j] = 1; //活细胞数==2,3
                    else temp[i][j] = 0;           //周围活细胞数>3
                } else if (cells == 3) temp[i][j] = 1; //死细胞但是周围正好有3个活细胞
            }
        }
        board = temp;
    }
};

第一次提交结果如下:

执行用时:0 ms, 在所有 C++ 提交中击败了100.00% 的用户
内存消耗:7.4 MB, 在所有 C++ 提交中击败了7.53% 的用户

解法2 原地工作

想要做到不使用额外空间,就要利用好输入。这题的输入是 vector<vector<int>>int 可以存储更多的比特位——让原有的最低位存储当前状态,第二低位存储下个状态,最后全部右移一位即可。相当于原有的状态只有 00, 01 ,现在新增加了 10, 11 两个状态。具体代码如下:

class Solution {
public:
    void gameOfLife(vector<vector<int>>& board) {
        if (board.empty()) return;
        int n = board.size(), m = board[0].size();
        /*
        00: dead (next state) <- dead (current state)
        01: dead (next state) <- live (current state) 
        10: live (next state) <- dead (current state)
        11: live (next state) <- live (current state) 
        */
        int Moves[][2] = {0, 1, 0, -1, 1, 0, -1, 0, -1, -1, -1, 1, 1, -1, 1, 1};
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                int lives = 0;
                for (int k = 0; k < 8; ++k) {
                    int x = i + Moves[k][0], y = j + Moves[k][1]; //查看周围细胞的状态(01,11活细胞)
                    if (x >= 0 && x < n && y >= 0 && y < m) lives += (board[x][y] & 1); 
                }
                if (board[i][j] & 1) { //当前活细胞,状态是0b01
                    if (2 <= lives && lives <= 3) board[i][j] = 3; //活细胞数为2/3,仍是活细胞0b11 
                    //else board[i][j] = 1;  //周围活细胞数<2或者>3,下个状态是死细胞0b01,不用赋值
                } else if (lives == 3) board[i][j] = 2; //当前死细胞,周围正好有3个活细胞,转为活细胞0b10
            }
        }
        for (int i = 0; i < n; ++i) 
            for (int j = 0; j < m; ++j)
                board[i][j] >>= 1; //全部右移一位
    }
};

空间效率提升不算多:

执行用时:0 ms, 在所有 C++ 提交中击败了100.00% 的用户
内存消耗:7.1 MB, 在所有 C++ 提交中击败了33.16% 的用户
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

memcpy0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值