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.

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?

题目标签: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 (生命游戏)的更多相关文章

  1. [LeetCode] 289. Game of Life 生命游戏

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

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

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

  3. Java实现 LeetCode 289 生命游戏

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

  4. Leetcode 289.生命游戏

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

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

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

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

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

  7. [LeetCode] Game of Life 生命游戏

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

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

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

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

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

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

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

随机推荐

  1. Python学习笔记004_字典_集合

    >>> # 字典 用大括号表示, 它是影射类型,相当于java中的Map >>> >>> dict1 = {'李宁': '一切皆有可能', '耐克 ...

  2. Pagination(分页) 从前台到后端总结

    一:效果图 下面我先上网页前台和管理端的部分分页效果图,他们用的是一套代码.                                   回到顶部(go to top) 二:上代码前的一些知识 ...

  3. GCD之线程挂起与恢复

    我们可以使用dispatch_suspend函数暂停一个queue以阻止它执行block对象;使用dispatch_resume函数继续dispatch queue.调用dispatch_suspen ...

  4. 【个人笔记】《知了堂》MySQL中的数据类型

    MySQL中的数据类型 1.整型 MySQL数据类型 含义(有符号) tinyint(m) 1个字节  范围(-128~127) smallint(m) 2个字节  范围(-32768~32767) ...

  5. TCP/IP(八)之总结ICP/IP四层模型

    前言 在这里有一个问题,有的书上说TCP/IP是四层有的却说是五层.其实这个问题我也上网查了一下资料. tcp/ip是事实标准,分4层.osi模型是国际标准,分7层.讲课的时候,一般把他们综合起来讲, ...

  6. The Moving Points hdu4717

    The Moving Points Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  7. (转)simhash算法原理及实现

    simhash是google用来处理海量文本去重的算法. google出品,你懂的. simhash最牛逼的一点就是将一个文档,最后转换成一个64位的字节,暂且称之为特征字,然后判断重复只需要判断他们 ...

  8. 20.Linux-USB鼠标驱动

    在上一章分析完USB总线驱动程序后, 接下来开始写一个USB驱动: 本节目的: 将USB鼠标的左键当作L按键,将USB鼠标的右键当作S按键,中键当作回车按键 参考/drivers/hid/usbhid ...

  9. Jquery几行代码解决跟随屏幕滚动DIV

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. Window window = Window.GetWindow(控件)

    Window window = Window.GetWindow(控件)