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?

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

类似题目:

Set Matrix Zeroes

参考资料:

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] Game of Life 生命游戏的更多相关文章

  1. [Leetcode] 第289题 生命游戏

    一.题目描述 根据百度百科,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在1970年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞.每个细胞具有一个初 ...

  2. 【LeetCode】Game of Life(生命游戏)

    这道题是LeetCode里的第289道题. 题目描述: 根据百度百科,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在1970年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格 ...

  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. Leetcode 289.生命游戏

    生命游戏 根据百度百科,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在1970年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞.每个细胞具有一个初始状 ...

  5. LeetCode | 289. 生命游戏(原地算法/位运算)

    记录dalao的位运算骚操作 根据百度百科 ,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在 1970 年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细 ...

  6. Java实现 LeetCode 289 生命游戏

    289. 生命游戏 根据百度百科,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在1970年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞.每个细胞具有 ...

  7. [Swift]LeetCode289. 生命游戏 | Game of Life

    According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellul ...

  8. React项目(二):生命游戏

    引子 这是16年最后的一个练手项目,一贯的感觉就是,做项目容易,写说明文档难.更何况是一个唤起抑郁感觉的项目,码下的每个字,心就如加了一个千斤的砝码. 2016年,有些事我都已忘记,但我现在还记得.2 ...

  9. 生命游戏/Game of Life的Java实现(转)

    首先简单介绍一下<生命游戏> 生命游戏其实是一个零玩家游戏.它包括一个二维矩形世界,这个世界中的每个方格居住着一个活着的或死了的细胞.一个细胞在下一个时刻生死取决于相邻八个方格中活着的或死 ...

随机推荐

  1. GIS部分理论知识备忘随笔

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.高斯克吕格投影带换算 某坐标的经度为112度,其投影的6度带和3度带 ...

  2. .NET4.5新特性之异步编程(Async和Await)的使用

    一.简介 首先来看看.net的发展中的各个阶段的特性:NET 与C# 的每个版本发布都是有一个"主题".即:C#1.0托管代码→C#2.0泛型→C#3.0LINQ→C#4.0动态语 ...

  3. JDBC 制作简单的登录验证

    两种方法: 一.直接拼接到SQL语句 public static void main(String[] args) throws Exception{ //输入账号密码 Scanner sc = ne ...

  4. python学习笔记(字符串操作、字典操作、三级菜单实例)

    字符串操作 name = "alex" print(name.capitalize()) #首字母大写 name = "my name is alex" pri ...

  5. Docker的ubuntu镜像安装的容器无ifconfig和ping命令的解决

    Docker的Ubuntu镜像安装的容器无ifconfig命令和ping命令 解决: apt-get update apt install net-tools       # ifconfig apt ...

  6. Mybatis的基本操作案列增加以及源码的分析(二)

    一.构建一个框架的项目的思路 首先我们先建立一个web项目,我们需要jar,mybatis-config.xml和studentDao.xml的配置随后就是dao.daoimpl.entity.的架构 ...

  7. 如何用Web技术开发Windows Form应用

    现在H5很热,很多互联网公司的产品都采用混合编程,其中各个平台客户端的“壳”为原生控件,但是内容很多都是Web网页,因此可以做出很多炫酷的效果.随着Node.js和Ionic等框架的出现,现在感觉Ja ...

  8. 移动Web利器transformjs入门

    简介 在过去的两年,越来越多的同事.朋友和其他不认识的童鞋进行移动web开发的时候,都使用了transformjs,所有必要介绍一下,让更多的人受益,提高编程效率,并享受编程乐趣.(当然transfo ...

  9. hexo博客-性能优化

    前言 刚开始搭建博客的时候觉得很好玩,可是玩的久了,问题慢慢就出来了,就跟谈恋爱一样==.比如现在我访问博客的时候就感觉慢的要死,不可否认,使用hexo搭建服务器方便快捷,但是由于github作为服务 ...

  10. Mac 下使用homebrew 安装node后全局安装找不到问题

    Homebrew 是mac上的包管理工具,其官网: http://brew.sh/ 在使用brew安装node之后安装一些常见工具比如 gulp npm install -g gulp 提示安装成功之 ...