Game of Life

406 篇文章 0 订阅
406 篇文章 0 订阅

1,题目要求

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:

  1. 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.
  2. 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?

根据维基百科的文章:“生命的游戏,也简称为生命,是由英国数学家约翰霍顿康威在1970年设计的细胞自动机。”

给定具有m×n个单元的板,每个单元具有初始状态live(1)或dead(0)。每个单元格使用以下四个规则(取自上述维基百科文章)与其八个邻居(水平,垂直,对角线)进行交互:

  1. 任何活着的邻居少于两个的活细胞都会死亡,好像是由于人口不足造成的。
  2. 任何有两三个活着的邻居的活细胞都会生活在下一代。
  3. 任何有三个以上活着的邻居的活细胞都会死亡,就好像人口过多一样。
  4. 任何具有正好三个活邻居的死细胞变成活细胞,就好像通过繁殖一样。

编写一个函数来计算给定其当前状态的板的下一个状态(在一次更新之后)。通过将上述规则同时应用于当前状态中的每个细胞来创建下一个状态,其中出生和死亡同时发生。

跟进:

  1. 你能在原地解决吗? 请记住,需要同时更新板:您不能先更新某些单元格,然后使用更新的值更新其他单元格。
  2. 在这个问题中,我们使用2D阵列代表板。 原则上,板是无限的,当活动区域侵入阵列的边界时会引起问题。 你会如何解决这些问题?

2,题目思路

对于这道题,要求判断一个二维阵列的在特定要求下的细胞繁殖情况。

总的来说,不是很难,直接对每个节点的周围的周围节点的个数进行判断即可。
不过因为每次繁殖,是一种情况的判断,而不是按次数发生的,因此,一般来说我们需要开辟额外的空间来记录这些细胞的繁殖情况。

而如果我们想要不开辟新空间,就需要将每次的计算结果在当前位置记录的前提下,还不能影响之后的节点利用该节点的值进行判断和计算。

因此,这里我们利用题目每个节点的值只能是0和1,以及位运算的特点,利用数字不同的位,来记录判断结果。
最后,在利用右移运算,得到每个位置的结果即可。

3,代码实现

static auto speedup = [](){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    return nullptr;
}();

class Solution {
public:
    void gameOfLife(vector<vector<int>>& board) {
        int m = board.size();
        if(m == 0)
            return;
        int n = board[0].size();
        
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                int count = 0;
                for(int ii=max(i-1,0);ii<min(i+2,m);ii++)
                    for(int jj=max(j-1,0);jj<min(j+2,n);jj++)
                        count += board[ii][jj] & 1;
                if(count == 3 || count - board[i][j] == 3)
                    board[i][j] |= 2;
            }
        }
        
        for(auto &b : board)
            for(auto &bb : b)
                bb >>= 1;
        return;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值