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):
- Any live cell with fewer than two live neighbors dies, as if caused by under-population.
- Any live cell with two or three live neighbors lives on to the next generation.
- Any live cell with more than three live neighbors dies, as if by over-population..
- 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:
- 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.
- 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),题目给出了细胞更新的规则,求出更新后的矩阵。规则如下:
- 如果活细胞周围八个位置的活细胞数少于两个,则该位置活细胞死亡;
- 如果活细胞周围八个位置有两个或三个活细胞,则该位置活细胞仍然存活;
- 如果活细胞周围八个位置有超过三个活细胞,则该位置活细胞死亡;
- 如果死细胞周围正好有三个活细胞,则该位置死细胞复活;
首先先判断元素是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++)的更多相关文章
- leetcode@ [289] Game of Life (Array)
https://leetcode.com/problems/game-of-life/ According to the Wikipedia's article: "The Game of ...
- [LeetCode] 289. Game of Life 生命游戏
According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellul ...
- LeetCode 289. Game of Life (生命游戏)
According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellul ...
- Java实现 LeetCode 289 生命游戏
289. 生命游戏 根据百度百科,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在1970年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞.每个细胞具有 ...
- Leetcode 289 Game of Life
According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellul ...
- Leetcode 289.生命游戏
生命游戏 根据百度百科,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在1970年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞.每个细胞具有一个初始状 ...
- leetcode 289生命游戏
class Solution { public: vector<vector<,},{,},{,},{,-},{,-},{-,-},{-,},{-,}}; void gameOfLife( ...
- LeetCode | 289. 生命游戏(原地算法/位运算)
记录dalao的位运算骚操作 根据百度百科 ,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在 1970 年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细 ...
- 2017-3-9 leetcode 283 287 289
今天操作系统课,没能安心睡懒觉23333,妹抖龙更新,可惜感觉水分不少....怀念追RE0的感觉 =================================================== ...
随机推荐
- 底层文件I/O操作中read()函数的缓存问题
最近在学习Linux过程中看到文件I/O操作这里时,文件I/O操作的系统调用涉及的5个函数:open(),read(),write(),lseek(),close().在一开始就阐明这些函数的特点是不 ...
- mybatis中SQL语句运用总结
union 连接查询 连接两个表后会过滤掉重复的值 <resultMap id="BaseResultMap" type="com.sprucetec.pay.e ...
- linux介绍及基本命令
linux简介 Linux内核最初只是由芬兰人李纳斯·托瓦兹(Linus Torvalds)在赫尔辛基大学上学时出于个人爱好而编写的. Linux是一套免费使用和自由传播的类Unix操作系统,是一个基 ...
- Spark Streaming核心概念与编程
Spark Streaming核心概念与编程 1. 核心概念 StreamingContext Create StreamingContext import org.apache.spark._ im ...
- 分别给Python类和实例增加属性和方法
#定义一个类Student class Student(object): pass #给类增加一个属性name Student.name = 'xm' print Student.name # xm ...
- 浅谈style.height、clientHeight、offsetHeight、scrollHeight
先分别介绍以下,以下资料来自MDN HTMLElement.offsetHeight 是一个只读属性,它返回该元素的像素高度,高度包含该元素的垂直内边距和边框,且是一个整数. Element.clie ...
- (转)ASP.NET中常见文件类型及用途
从入门导师那继承来的习惯,也是加上自己的所谓经验判断,一直对WEB开发不太感冒,可惜呀,从业近二十年,还得从头开始对付HTML.CSS.JS.ASPX,以前的经验,用不上啦!!!先从好好学习ASPX开 ...
- Linux入门进阶第三天——软件安装管理(下)
一.yum在线安装 之前的rpm包各种依赖性太强!安装复杂,yum的好处就来了: // yum 在redhat是付费服务 1.yum源文件 先进入到yum目录: 我们打开默认生效的Base包 2.光盘 ...
- 20155322 2016-2017-2 《Java面向对象程序设计》第十二周课堂练习之Arrays和String单元测试
20155322 2016-2017-2 <Java面向对象程序设计>第十二周课堂练习之Arrays和String单元测试 练习目地 在IDEA中以TDD的方式对String类和Array ...
- 20155323 2016-2017-2 《Java程序设计》第2周学习总结
20155323 2016-2017-2 <Java程序设计>第2周学习总结 教材学习内容总结 对象:对象是类的一个实例,有状态和行为. 类:类是一个模板,它描述一类对象的行为和状态. 第 ...