LeetCode 289. Game of Life (C++)

题目:

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?

分析:

矩阵中每个元素为1(live)或0(dead),题目给出了细胞更新的规则,求出更新后的矩阵。规则如下:

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

首先先判断元素是0还是1,再计算周围八个元素的值的和,根据规则更新。创建一个新的二维数组,最后将计算的值赋给原数组。

程序:

class Solution {
public:
    void gameOfLife(vector<vector<int>>& board) {
        vector<vector<int>> res;
        vector<int> temp;
        for (int i = 0; i < board.size(); i++){
            for (int j = 0; j < board[0].size(); j++){
                //dead cell
                if (board[i][j] == 0){
                    int alive = 0;
                    for (int m = i-1; m <= i+1; m++){
                        for (int n = j-1; n <= j+1; n++){
                            if (m < 0 || m >= board.size() || n < 0 || n >= board[0].size())
                                continue;
                            else{
                                if (board[m][n] == 1)
                                    alive++;
                            }
                        }
                    }
                    if (alive == 3)
                        temp.push_back(1);
                    else
                        temp.push_back(0);
                }
                //live cell
                else{
                    int al = 0;
                    for (int m = i-1; m <= i+1; m++){
                        for (int n = j-1; n <= j+1; n++){
                            if (m < 0 || m >= board.size() || n < 0 || n >= board[0].size())
                                continue;
                            else{
                                if (board[m][n] == 1)
                                    al++;
                            }
                        }
                    }
                    //计算了board[m][n]的值,所以判断条件+1
                    if (al < 3)
                        temp.push_back(0);
                    else if (al > 4)
                        temp.push_back(0);
                    else
                        temp.push_back(1);
                }
            }
            res.push_back(temp);
            temp.clear();
        }
        for (int i = 0; i < board.size(); i++){
            for (int j = 0; j < board[0].size(); j++){
                board[i][j] = res[i][j];
            }
        }
    }
};

备注:

做法是新开辟了一个数组,以后再更新下用原数组的程序。

原文地址:https://www.cnblogs.com/silentteller/p/10192457.html

时间: 2024-08-30 06:58:48

LeetCode 289. Game of Life (C++)的相关文章

LeetCode 289 - 生命游戏 - 原地算法

Wikipedia关于原地算法的描述:原地算法(in-place algorithm)基本上不需要额外辅助的数据结构,然而,允许少量额外的辅助变量来转换数据的算法.当算法运行时,输入的数据通常会被要输出的部分覆盖掉. 很容易看出,原地算法的特点是不需要辅助的数据结构而只需要辅助变量.通常,维护一个复杂的数据结构时间空间复杂度都是比较高的,原地算法的优越性正来自于不依赖数据结构,尽管它借鉴了数据结构的思想. 同时也不难看出,原地算法总体的实现思路就是用输出的数据在原地覆盖已经“没用”的输入的数据.

[leetcode] 289. Game of Life 解题报告

题目链接: https://leetcode.com/problems/game-of-life/ 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 

Leetcode 289 Game of Life

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

Leetcode 289.生命游戏

生命游戏 根据百度百科,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在1970年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞.每个细胞具有一个初始状态 live(1)即为活细胞, 或 dead(0)即为死细胞.每个细胞与其八个相邻位置(水平,垂直,对角线)的细胞都遵循以下四条生存定律: 如果活细胞周围八个位置的活细胞数少于两个,则该位置活细胞死亡: 如果活细胞周围八个位置有两个或三个活细胞,则该位置活细胞仍然存活: 如果活细胞周围八个位置有超过

[email&#160;protected] [289] Game of Life

https://leetcode.com/problems/game-of-life/ 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 c

LeetCode:Game of Life - 康威生命游戏

1.题目名称 Game of Life(康威生命游戏) 2.题目地址 https://leetcode.com/problems/game-of-life 3.题目内容 英文: 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 Conw

【leetcode】289. Game of Life

题目如下: 解题思路:因为本题要求的是直接在输入的数组上面修改,而且细胞的生死转换是一瞬间的事情,所以需要引入两个中间状态,待死和待活.这两个中间状态用于在四条规则判断的时候是"活"和"死",但是呈现在最终的结果是"死"和"活". 代码如下: class Solution(object): def getLiveCount(self,board,x,y): l = [(0,-1),(0,1),(-1,0),(1,0),(-1,

[Leetcode] 第289题 生命游戏

一.题目描述 根据百度百科,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在1970年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞.每个细胞具有一个初始状态 live(1)即为活细胞, 或 dead(0)即为死细胞.每个细胞与其八个相邻位置(水平,垂直,对角线)的细胞都遵循以下四条生存定律: 如果活细胞周围八个位置的活细胞数少于两个,则该位置活细胞死亡: 如果活细胞周围八个位置有两个或三个活细胞,则该位置活细胞仍然存活: 如果活细胞周围八个位置有

73. Set Matrix Zeroes &amp;&amp; 289. Game of Life

73. Set Matrix Zeroes Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Hide Tags Array Hide Similar Problems (M) Game of Life public class Solution { //Only consider the zeros that exist originally. public