题目:

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. Apache安装排错

    今天安装一下Apache,发现报错,且在网上没有找到相关解决方法,所以记录一下 安装步骤:将下载好的apache包放置到要放置的目录中,最好是盘根目录下,然后命令行下进入到apache下面的bin目录 ...

  2. svg路径动画心得

    svg动画,随着路线运动,项目中需要用到,接触的时候感觉很高级,但是不会-无从下手呀!于是在网上找相关资料,先借鉴再修改成自己的. <svg width="500" heig ...

  3. mybatis中SQL语句运用总结

    union 连接查询  连接两个表后会过滤掉重复的值 <resultMap id="BaseResultMap" type="com.sprucetec.pay.e ...

  4. Python - 魔法字符串

    ''' #capitalize() ---首字母转换为大写--- s="sslssd" v=s.capitalize(); print(v) ''' ''' #center(20, ...

  5. pyhton3解决"tuple parameter unpacking is not supported"问题

    准备将键值对中的键与值对调,结果第10行出了bug,显示"tuple parameter unpacking is not supported" 解决方法:将map(lambda( ...

  6. spark 例子wordcount topk

    spark 例子wordcount topk 例子描述: [单词计算wordcount ] [词频排序topk] 单词计算在代码方便很简单,基本大体就三个步骤 拆分字符串 以需要进行记数的单位为K,自 ...

  7. Linux入门第五天——shell脚本入门(下)基础语法之调试debug

    一.如何debug 1.通过sh命令的参数: sh [-nvx] scripts.sh 选项与参数: -n :不要执行 script,仅查询语法的问题: -v :再执行 sccript 前,先将 sc ...

  8. 20155325 2016-2017-2 《Java程序设计》课程总结

    (按顺序)每周作业链接汇总 预备作业1:浅谈对师生关系的看法以及对未来学习生活的展望 预备作业2:学习娄老师<做中学>系列文章.自身C语言情况.Java课程目标 预备作业3:安装虚拟机情况 ...

  9. 20155339 2016-2017-2 《Java程序设计》第十周学习总结

    20155339 2016-2017-2 <Java程序设计>第十周学习总结 教材学习内容总结 计算机网络概述 在计算机网络中,现在命名IP地址的规定是IPv4协议,该协议规定每个IP地址 ...

  10. day 14 元组

    1. 使用场景? # 列表list 数据类型相同, #rwx文件 100个人的名字, # 用字典 dict ['dɪkt] 很多信息描述1个人, # tuple [ˈtʌpəl] #只读文件 不能修改 ...