题目:

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 = ; i < board.size(); i++){
for (int j = ; j < board[].size(); j++){
//dead cell
if (board[i][j] == ){
int alive = ;
for (int m = i-; m <= i+; m++){
for (int n = j-; n <= j+; n++){
if (m < || m >= board.size() || n < || n >= board[].size())
continue;
else{
if (board[m][n] == )
alive++;
}
}
}
if (alive == )
temp.push_back();
else
temp.push_back();
}
//live cell
else{
int al = ;
for (int m = i-; m <= i+; m++){
for (int n = j-; n <= j+; n++){
if (m < || m >= board.size() || n < || n >= board[].size())
continue;
else{
if (board[m][n] == )
al++;
}
}
}
//计算了board[m][n]的值,所以判断条件+1
if (al < )
temp.push_back();
else if (al > )
temp.push_back();
else
temp.push_back();
}
}
res.push_back(temp);
temp.clear();
}
for (int i = ; i < board.size(); i++){
for (int j = ; j < board[].size(); j++){
board[i][j] = res[i][j];
}
}
}
};

备注:

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

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

  1. leetcode@ [289] Game of Life (Array)

    https://leetcode.com/problems/game-of-life/ According to the Wikipedia's article: "The Game of ...

  2. [LeetCode] 289. Game of Life 生命游戏

    According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellul ...

  3. LeetCode 289. Game of Life (生命游戏)

    According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellul ...

  4. Java实现 LeetCode 289 生命游戏

    289. 生命游戏 根据百度百科,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在1970年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞.每个细胞具有 ...

  5. Leetcode 289 Game of Life

    According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellul ...

  6. Leetcode 289.生命游戏

    生命游戏 根据百度百科,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在1970年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞.每个细胞具有一个初始状 ...

  7. leetcode 289生命游戏

    class Solution { public: vector<vector<,},{,},{,},{,-},{,-},{-,-},{-,},{-,}}; void gameOfLife( ...

  8. LeetCode | 289. 生命游戏(原地算法/位运算)

    记录dalao的位运算骚操作 根据百度百科 ,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在 1970 年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细 ...

  9. 2017-3-9 leetcode 283 287 289

    今天操作系统课,没能安心睡懒觉23333,妹抖龙更新,可惜感觉水分不少....怀念追RE0的感觉 =================================================== ...

随机推荐

  1. 【CSU 1803】2016 (数学)

    Description 给出正整数 n 和 m,统计满足以下条件的正整数对 (a,b) 的数量: 1. 1≤a≤n,1≤b≤m; 2. a×b 是 2016 的倍数. Input 输入包含不超过 30 ...

  2. macOS,安装+配置+激活:MySQL8.0 + Navicat Premium12

    作者的电脑是10.13.3,些许配置偏差请自行略过 本文是学习探讨途径,请勿滥用,后果自负 MySQL8.0 篇章 官网http://www.mysql.com/downloads/ 下载即可,无需激 ...

  3. thinphp5-image图片处理类库压缩图片

    使用tp5的thinkphp-image类库处理图片 使用方法手册都有,为了增加印象我自己记录一下 手册:https://www.kancloud.cn/manual/thinkphp5/177530 ...

  4. 关于STM32 DMA相关总结[概述知识点]

    关于DMA相关知识的总结,写给未来的自己,希望有帮助.立个Flag[坚持写博客总结自己工作或学习记录自己的生活] ------------------------------------------- ...

  5. ubuntu系统部署python3.6.4

    Ubuntu的版本为16.04,系统自带的Python版本较低,使用亲本版本3.6.4,下为安装步骤: 一.官网下载Python3.6.4版本 新建目录: sudo mkidr /usr/local/ ...

  6. 希尔伯特曲线——第八届蓝桥杯C语言B组(国赛)第三题

    原创 标题:希尔伯特曲线 希尔伯特曲线是以下一系列分形曲线 Hn 的极限.我们可以把 Hn 看作一条覆盖 2^n × 2^n 方格矩阵的曲线,曲线上一共有 2^n × 2^n 个顶点(包括左下角起点和 ...

  7. Winform程序拖拽文件到窗体

    1:首先需要将接收拖拽的窗体属性AllowDrop设置为True. 2:编写窗体拖拽进入(DragEnter)和拖拽完成(DragDrop)事件. private void FrmCode_DragE ...

  8. 20145207李祉昂《网络对抗技术》可选实验 shellcode注入与Return-to-libc攻击实验

    1.0 实践内容 Return-to-libc攻击是一种特殊的缓冲区溢出攻击,通常用于攻击有“栈不可执行”保护措施的目标系统.本实验中我们放弃了让漏洞程序执行堆栈中的shellcode,将用syste ...

  9. print puts p

    共同点:都是用来屏幕输出的. 不同点:puts 输出内容后,会自动换行(如果内容参数为空,则仅输出一个换行符号):另外如果内容参数中有转义符,输出时将先处理转义再输出p 基本与puts相同,但不会处理 ...

  10. day 5 多态 类 静态

    1.多态 执行的时候才知道调用谁 class Dog(object): def print_self(self): print("大家好,我是来自西安的小白") class Xia ...