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.
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?
题目标签:Array
题目给了我们一个2d array,让我们根据game of life 的规则,改变cells,进行到下一个 状态。
规定了我们要in-place改动,但是问题在于,每一个cell 的改动都依赖于它周围8个的cell的情况,所以有先后顺序的改动,肯定会造成错误。
所以我们要想出一种方法,就是,改动完了的cell,我们查看它的情况,依然能够知道那个cell 之前的state。
对于每一个cell, 可以建立 4种情况:
0 dead -> dead 没有变化
1 live -> live 没有变化
2 live -> dead 从live 变为 dead
3 dead -> live 从dead 变为live
对于情况0 和1, 因为没有变化,所以每个cell 的值 还是 0 和 1;
增加2种情况:如果是从 live 变为 dead, 就把值改成2; 如果是从 dead 变为 live, 就把值改成3。
那么对于每一个cell, 我们要查看的是它周围有几个live cell 来做判断,如果周围的cell 已经被改动过了,我们需要看的是它之前的状态。
那么我们看,红色的是初始的状态,蓝色的是改动过的状态,因为我们只需要查看live, 不管它有没有被改动过,我们只看红色部分就对了,因为我们要的是它初始的状态,那么只可能是1 和 2。所以我们只需要查看所有cell 是1 或者是 2的,来count 一共有几个live neighbors。
Java Solution:
Runtime beats 10.94%
完成日期:09/15/2017
关键词:Array,
关键点:对于每一个cell,有4个值,每一个值对应一种变化关系
class Solution
{
public void gameOfLife(int[][] board)
{
if(board == null || board.length == 0)
return; int m = board.length; // rows
int n = board[0].length; // columns // first iteration: mark states for each cell
for(int i=0; i<m; i++) // rows
{
for(int j=0; j<n; j++) // columns
{
int cnt = 0;
// count cell's live neighbors 3x3 matrix and set boundary
for(int x= Math.max(0, i-1); x<= Math.min(m-1, i+1); x++)
{
for(int y= Math.max(0, j-1); y<= Math.min(n-1, j+1); y++)
{
if(x == i && y == j) // skip itself
continue;
// only state 1 and 2: cell are live for previous state
if(board[x][y] == 1 || board[x][y] == 2)
cnt++;
}
} if(board[i][j] == 0 && cnt == 3) // current is dead cell
board[i][j] = 3; // dead -> live
else if(board[i][j] == 1 && (cnt < 2 || cnt > 3)) // current live cell
board[i][j] = 2; // live -> dead
}
} // second iteration: convert state back to dead or live
for(int i=0; i<m; i++)
for(int j=0; j<n; j++)
board[i][j] %= 2;
}
}
参考资料:
http://www.cnblogs.com/grandyang/p/4854466.html
LeetCode 题目列表 - LeetCode Questions List
LeetCode 289. Game of Life (生命游戏)的更多相关文章
- [LeetCode] 289. Game of Life 生命游戏
According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellul ...
- 【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实现(转)
首先简单介绍一下<生命游戏> 生命游戏其实是一个零玩家游戏.它包括一个二维矩形世界,这个世界中的每个方格居住着一个活着的或死了的细胞.一个细胞在下一个时刻生死取决于相邻八个方格中活着的或死 ...
随机推荐
- latch session allocation
应用反馈上午10点左右出现大量应用连接数据库报错 采集9点-10点和10点-11点的AWR报告进行分析 DB时间明显差异,再继续分析等待事件 可以看出有session相关的Latch等待事件,查看相关 ...
- 【翻译】基于web创建逼真的3D图形 | CSS技巧
个人翻译小站:http://www.zcfy.cc/article/creating-photorealistic-3d-graphics-on-the-web-css-tricks-4039.htm ...
- 【京东详情页】——原生js学习之匿名函数
一.引言 在js模块中,要给每一个功能封装一个匿名函数.为了更好的理解什么是匿名函数,为什么要用匿名函数,我做了一些查阅和学习. 二.匿名函数 什么是:在创建时,不被任何变量引用的函数. 为什么:节约 ...
- 为什么要用深度学习来做个性化推荐 CTR 预估
欢迎大家前往腾讯云技术社区,获取更多腾讯海量技术实践干货哦~ 作者:苏博览 深度学习应该这一两年计算机圈子里最热的一个词了.基于深度学习,工程师们在图像,语音,NLP等领域都取得了令人振奋的进展.而深 ...
- DOM中元素对象的属性方法
在 HTML DOM (文档对象模型)中,每个部分都是节点. 节点是DOM结构中最基本的组成单元,每一个HTML标签都是DOM结构的节点. 文档是一个 文档节点 . 所有的HTML元素都是 ...
- tomcat部署在centos6.8上的乱码问题
web访问经常会莫名其妙的出现各种乱码问题.按照我自己的理解,设置一个charSet的过滤器,代码如下:import java.io.IOException; import javax.servlet ...
- ThinkPHP中:RBAC权限控制的实习步骤
使用版本ThinkPHP3.1.3 第一步,建表及数据 第二步,建关联模型 第三步,控制器使用关联模型.配置文件 第四步,模板显示数据 第一步,建表及数据 在数据库中,建立一个companysvn数据 ...
- 在CentOS6上利用PXE+Kickstart+Apache+DHCP实现无人值守安装
在CentOS6上利用PXE+Kickstart+Apache+DHCP实现无人值守安装 1.PXEServer:OS:CentOS6.9IP:172.16.25.69: (1)apache:# mo ...
- PHP合并两张图片(水印)
$dst_im = "http://img6.cache.netease.com/photo/0001/2016-04-15/BKMTUO8900AP0001.jpg"; $src ...
- C语言判断电脑的大、小端机
#include int main() { int x = 0x1234; if (char(x) == 0x34) { printf("小端机!\n"); } else ...