题目:

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. Hadoop启动dataNode失败,却没有任何报错

    问题描述: centos7,伪分布模式下,启动datanode后,通过JPS查看发现没有相关进程,在日志文件里也没有任何提示.通过百度,网上一堆说什么vesion 的ID不一致,不能解决我的问题. 经 ...

  2. 在UIWindow上加类似于“回到顶部”的按钮

    在公司上个版本的开发中遇到了一个UI布局的小问题: 某个页面需要增加一个分享按钮,但是该页面是二级页面,导航栏右边也已经放置了2个button. 起初和老大谈论这个问题的时候想到的方法是导航栏右边加三 ...

  3. 【gitlab平台的搭建】

    gitlab同github相同,具有把源码集中存放的功能,同时依靠git进行code的同步,在实际的开发过程中可保证团队的项目同步,同时便于便于维护等 #下载这个rpm包 #gitlab.rb访问地址 ...

  4. windows提权之前的信息收集

    0x00 基本信息 -获取主机名:hostname或者echo %COMPUTERNAME% -获取所属域信息:systeminfo 获取环境变量:set 0x01 获取系统安装的软件信息 -导出注册 ...

  5. (数据科学学习手札49)Scala中的模式匹配

    一.简介 Scala中的模式匹配类似Java中的switch语句,且更加稳健,本文就将针对Scala中模式匹配的一些基本实例进行介绍: 二.Scala中的模式匹配 2.1 基本格式 Scala中模式匹 ...

  6. 团队展示网页 HTML模版

    之前帮着领导,参加了iGEM的校内赛的网页制作,一开始也是用的现成的模版,但后面修修改改几乎面目全非了- 这里分享一下自己的网站,可以用做团队展示的网页模版,文件在末尾,大家自行下载吧-- 这里贴两张 ...

  7. 20155330 2016-2017-2 《Java程序设计》第四周学习总结

    20155330 2016-2017-2 <Java程序设计>第四周学习总结 教材学习内容总结 学习目标 理解封装.继承.多态的关系 理解抽象类与接口的区别 掌握S.O.L.I.D原则 了 ...

  8. 【BZOJ2754】[SCOI2012]喵星球上的点名

    [BZOJ2754][SCOI2012]喵星球上的点名 题面 bzoj 洛谷 题解 这题有各种神仙做法啊,什么暴力\(AC\)自动机.\(SAM\)等等五花八门 我这个蒟蒻在这里提供一种复杂度正确且常 ...

  9. 海思NB-IOT的SDK函数使用说明

    1. 查询当前AT指令是否正在处理中 while(get_at_cmd_in_progress() == false); 2. 信号量发送函数 (, osNoWait); 3. 信号量接收函数 if( ...

  10. (转载)C#提取汉字拼音首字母的方法

    今天突然要用到提取汉字拼音首字母的功能,去网上找了找,发现没有几个好用的,决定自己写一个,效果还不错,发出来大家一起研究下,分享给大家!直接入主题: 1.首先对编码进行定义 #region 编码定义 ...