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的感觉 =================================================== ...
随机推荐
- iOS audio不支持循环播放
解决办法:监听播放完成事件(注意点,audio标签不能设置循环播放,去除标签 loop="loop"或者 loop="false",不然不走播放完成事件) $( ...
- 01.centos7环境准备
博客为日常工作学习积累总结: 1.环境准备: 系统版本:CentOS-7-x86_64-Minimal-1810.iso 运行环境:虚拟机windows上的VM 15 系统安装:参照老男孩运维要求 2 ...
- java 关于String
1.两种创建方式 String str1 = "abc"; //字面量创建 String str2 = new String("abc"); //构造方法创建 ...
- 001_02-python基础习题答案
python 基础习题 执行 Python 脚本的两种方式 如:脚本/python/test.py 第一种方式:python /python/test.py 第二中方式:在test.py中声明:/us ...
- redis迁移复制数据,主从关系建立实践
装redis的机器出了点问题,需要转移数据然后初始化系统,然后我就研究了下redis的数据复制,发现了slaveof 192.168.0.1 6379这个命令,开始踩下这个坑 首先要新的服务器上进入r ...
- 将python自动转换为.exe文件
使用py2exe包进行转换.py2exe怎么装的可以网上另查.时间久了,记不太清了...... 这个程序可以把自己进行转换.但是没法运行....其实只要是需要修改自身的程序打包后都没法运行. # -* ...
- python中Excel表操作
python中关于excel表个的操作 使用 python中的xlwt和xlrd模块进行操作 # 2003之前:Excel:xls# 2003之后:Excel:xlsx# xlrd:读取的模块:xls ...
- uni-app 下拉至指定高度固定view
uni.createSelectorQuery().select(‘#salyt’).boundingClientRect(function(rects){ console.log(rects) va ...
- 20155233 《Java程序设计》实验四 Android开发基础
20155233 <Java程序设计>实验四 Android开发基础 实验内容 1.基于Android Studio开发简单的Android应用并部署测试; 2.了解Android组件.布 ...
- 20155236 《Java程序设计》实验四(Android程序设计)实验报告
20155236 <Java程序设计>实验四(Android程序设计)实验报告 一.实验内容及步骤 第24章:初识Android 任务一:完成Hello World, 要求修改res目录中 ...