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实现(转)
首先简单介绍一下<生命游戏> 生命游戏其实是一个零玩家游戏.它包括一个二维矩形世界,这个世界中的每个方格居住着一个活着的或死了的细胞.一个细胞在下一个时刻生死取决于相邻八个方格中活着的或死 ...
随机推荐
- json:JSONObject包的具体使用(JSONObject-lib包是一个beans,collections,maps,java arrays和xml和JSON互相转换的包)
1.JSONObject介绍 JSONObject-lib包是一个beans,collections,maps,java arrays和xml和JSON互相转换的包. 2.下载jar包 http:// ...
- 如何实现Sublime Text3中vue文件高亮显示的最有效的方法
今天第一次使用Sublime Text3软件,在实现vue文件高亮显示的过程中一直报错,经过了半天时间的不停尝试终于找到了最有效的一种解决方法!错误提示如下: 刚开始尝试了很多方法都不行,只要打开in ...
- DOM【介绍、HTML中的DOM、XML中的DOM】
什么是DOM? DOM(Document Object Model)文档对象模型,是语言和平台的中立接口. 允许程序和脚本动态地访问和更新文档的内容. 为什么要使用DOM? Dom技术使得用户页面可以 ...
- Java Classloader机制解析(转)
做Java开发,对于ClassLoader的机制是必须要熟悉的基础知识,本文针对Java ClassLoader的机制做一个简要的总结.因为不同的JVM的实现不同,本文所描述的内容均只限于Hotspo ...
- 对比requirejs更好的理解seajs
seajs遵循CMD规范,requirejs遵循AMD规范.AMD规范是预加载,CMD规范是赖加载. 下文举例假设有文件 b.js, c.js如下 //b.js define(function(req ...
- ThinkJS框架入门详细教程(一)开发环境
一.前端标配环境 1.nodeJS正确安装,可以参考:http://www.cnblogs.com/chengxs/p/6221393.html 2.git正确安装,可以参考:http://www.c ...
- asp.net web api 2.2 基础框架(带例子)
链接:https://github.com/solenovex/asp.net-web-api-2.2-starter-template 简介 这个是我自己编写的asp.net web api 2.2 ...
- Android Studio 导入应用时报错 Error:java.lang.RuntimeException: Some file crunching failed, see logs for details
在app文件夹的build.gradle里加上 android { ...... aaptOptions.cruncherEnabled = false aaptOptions.useNewCrunc ...
- 在linux上安装rz、sz包
在SecureCRT这样的ssh登录软件里, 通过在Linux界面里输入rz/sz命令来上传/下载文件. 对于RHEL5, rz/sz默认没有安装所以需要手工安装.sz: 将选定的文件发送(send) ...
- 【转】wireshark基本用法及过虑规则
Wireshark 基本语法,基本使用方法,及包过虑规则: 1.过滤IP,如来源IP或者目标IP等于某个IP 例子: ip.src eq 192.168.1.107 or ip.dst eq 19 ...