[LeetCode] 289. Game of Life 生命游戏
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?
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
这道题是有名的 康威生命游戏, 而我又是第一次听说这个东东,这是一种细胞自动机,每一个位置有两种状态,1为活细胞,0为死细胞,对于每个位置都满足如下的条件:
1. 如果活细胞周围八个位置的活细胞数少于两个,则该位置活细胞死亡
2. 如果活细胞周围八个位置有两个或三个活细胞,则该位置活细胞仍然存活
3. 如果活细胞周围八个位置有超过三个活细胞,则该位置活细胞死亡
4. 如果死细胞周围正好有三个活细胞,则该位置死细胞复活
由于题目中要求用置换方法 in-place 来解题,所以就不能新建一个相同大小的数组,那么只能更新原有数组,题目中要求所有的位置必须被同时更新,但在循环程序中还是一个位置一个位置更新的,当一个位置更新了,这个位置成为其他位置的 neighbor 时,怎么知道其未更新的状态呢?可以使用状态机转换:
状态0: 死细胞转为死细胞
状态1: 活细胞转为活细胞
状态2: 活细胞转为死细胞
状态3: 死细胞转为活细胞
最后对所有状态对2取余,则状态0和2就变成死细胞,状态1和3就是活细胞,达成目的。先对原数组进行逐个扫描,对于每一个位置,扫描其周围八个位置,如果遇到状态1或2,就计数器累加1,扫完8个邻居,如果少于两个活细胞或者大于三个活细胞,而且当前位置是活细胞的话,标记状态2,如果正好有三个活细胞且当前是死细胞的话,标记状态3。完成一遍扫描后再对数据扫描一遍,对2取余变成我们想要的结果。参见代码如下:
class Solution {
public:
void gameOfLife(vector<vector<int> >& board) {
int m = board.size(), n = m ? board[].size() : ;
vector<int> dx{-, -, -, , , , , };
vector<int> dy{-, , , , , , -, -};
for (int i = ; i < m; ++i) {
for (int j = ; j < n; ++j) {
int cnt = ;
for (int k = ; k < ; ++k) {
int x = i + dx[k], y = j + dy[k];
if (x >= && x < m && y >= && y < n && (board[x][y] == || board[x][y] == )) {
++cnt;
}
}
if (board[i][j] && (cnt < || cnt > )) board[i][j] = ;
else if (!board[i][j] && cnt == ) board[i][j] = ;
}
}
for (int i = ; i < m; ++i) {
for (int j = ; j < n; ++j) {
board[i][j] %= ;
}
}
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/289
类似题目:
参考资料:
https://leetcode.com/problems/game-of-life/
https://leetcode.com/problems/game-of-life/discuss/73217/Infinite-board-solution
https://leetcode.com/problems/game-of-life/discuss/73230/C%2B%2B-O(1)-space-O(mn)-time
https://leetcode.com/problems/game-of-life/discuss/73223/Easiest-JAVA-solution-with-explanation
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 289. Game of Life 生命游戏的更多相关文章
- 【LeetCode】Game of Life(生命游戏)
这道题是LeetCode里的第289道题. 题目描述: 根据百度百科,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在1970年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格 ...
- Java实现 LeetCode 289 生命游戏
289. 生命游戏 根据百度百科,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在1970年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞.每个细胞具有 ...
- Leetcode 289.生命游戏
生命游戏 根据百度百科,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在1970年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞.每个细胞具有一个初始状 ...
- [Leetcode] 第289题 生命游戏
一.题目描述 根据百度百科,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在1970年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞.每个细胞具有一个初 ...
- LeetCode | 289. 生命游戏(原地算法/位运算)
记录dalao的位运算骚操作 根据百度百科 ,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在 1970 年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细 ...
- [LeetCode] Game of Life 生命游戏
According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellul ...
- [Swift]LeetCode289. 生命游戏 | Game of Life
According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellul ...
- React项目(二):生命游戏
引子 这是16年最后的一个练手项目,一贯的感觉就是,做项目容易,写说明文档难.更何况是一个唤起抑郁感觉的项目,码下的每个字,心就如加了一个千斤的砝码. 2016年,有些事我都已忘记,但我现在还记得.2 ...
- 生命游戏/Game of Life的Java实现(转)
首先简单介绍一下<生命游戏> 生命游戏其实是一个零玩家游戏.它包括一个二维矩形世界,这个世界中的每个方格居住着一个活着的或死了的细胞.一个细胞在下一个时刻生死取决于相邻八个方格中活着的或死 ...
随机推荐
- Unity Shader 屏幕后效果——摄像机运动模糊(速度映射图实现)
速度映射图主要是为了得到每个像素相对于前一帧的运动矢量,其中一种方法是使用摄像机的深度纹理来推导. 推导过程如下: 先由深度纹理逆推出NDC(归一化的设备坐标)下的顶点坐标,利用VP矩阵(视角*投影矩 ...
- 【mysql报错】MySQL5.7.27报错“[Warning] Using a password on the command line interface can be insecure.”
MySQL5.7.27报错“[Warning] Using a password on the command line interface can be insecure.”在命令行使用密码不安全警 ...
- PHP导出文件到csv函数
PHP导出文件到CSV函数 function exportCSV($data=array(),$title=array(),$filename) { $encoded_filename = urlen ...
- NRF24L01双向无线通信
最近闲来无事,利用手头资源研究了一下基于nrf24L01的双向通信实验,整个系统如下图所示. 原理: nrf24L01本身是一种单向通信的无线模块,但是,当nrf24L01工作在增强型的 ShockB ...
- easyui 扩展 datagrid 数据网格视图
效果如图: js代码: $("#tdg").datagrid({ width: 200, url: "/Laboratory/ ...
- Java面试复习(纯手打)
1.面向对象和面向过程的区别: 面向过程比面向对象高.因为类调用时需要实例化,开销比较大,比较消耗资源,所以当性能是最重要的考量因素得时候,比如单片机.嵌入式开发.Linux/Unix等一般采用面向过 ...
- electron——ipcMain模块、ipcRenderer模块
ipcMain 从 主进程 到 渲染进程 的异步通信. ipcMain模块是EventEmitter类的一个实例. 当在主进程中使用时,它处理从渲染器进程(网页)发送出来的异步和同步信息. 从渲染器进 ...
- 爬取70城房价到oracle数据库并6合1
学习数据分析,然后没有合适的数据源,从国家统计局的网页上抓取一页数据来玩玩(没有发现robots协议,也仅仅发出一次连接请求,不对网站造成任何负荷) 运行效果 源码 python代码 ''' 本脚本旨 ...
- github 分支管理
github 分支管理 最近有同事问我git 如何管理分支,这里我以github为例,做下工作中常用的分支管理操作. 分支管理 作用:假设你准备开发一个新功能,但需要两周才能完成,第一周写了60%,如 ...
- C语言三种参数传递方式
值传递.指针传递.引用传递 只有在函数调用时,才会为形参分配内存空间,调用结束便会释放. 值传递和指针传递,传递的都是实参的一份拷贝. C语言在线编译器:http://www.dooccn.com/c ...