Game of Life 解答
Question
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?
Solution
这题的关键在于合适的编码。我们用两位来代表每个cell的状态。
after prev
1 0 以前是dead,经过一次update后为live
1 1 以前是live,经过一次update后为live
0 1 以前是live,经过一次update后为dead
0 0 以前是dead,经过一次update后为dead
由此我们看到取得prev位可以用%操作,取得after位可以用/操作。
因此我们遍历两遍2维数组。第一遍是将所有元素编码成上述两位形式。第二遍是除法操作,得到after。
Time complexity O(mn), space cost O(1)
public class Solution {
public void gameOfLife(int[][] board) {
int[][] directions = {{-1,0},{1,0},{0,1},{0,-1},{-1,-1},{-1,1},{1,-1},{1,1}};
int m = board.length, n = board[0].length;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int prev = board[i][j];
int liveCount = 0, deadCount = 0;
for (int k = 0; k < 8; k++) {
int newX = i + directions[k][0];
int newY = j + directions[k][1];
if (newX < 0 || newX >= m || newY < 0 || newY >= n) {
continue;
}
if (board[newX][newY] % 2 == 1) {
liveCount++;
}
}
if (prev == 0) {
if (liveCount == 3) {
board[i][j] += 2;
}
} else {
if (liveCount == 2 || liveCount == 3) {
board[i][j] += 2;
}
}
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
board[i][j] = board[i][j] / 2;
}
}
}
}
Game of Life 解答的更多相关文章
- 精选30道Java笔试题解答
转自:http://www.cnblogs.com/lanxuezaipiao/p/3371224.html 都 是一些非常非常基础的题,是我最近参加各大IT公司笔试后靠记忆记下来的,经过整理献给与我 ...
- 精通Web Analytics 2.0 (8) 第六章:使用定性数据解答”为什么“的谜团
精通Web Analytics 2.0 : 用户中心科学与在线统计艺术 第六章:使用定性数据解答"为什么"的谜团 当我走进一家超市,我不希望员工会认出我或重新为我布置商店. 然而, ...
- 【字符编码】Java字符编码详细解答及问题探讨
一.前言 继上一篇写完字节编码内容后,现在分析在Java中各字符编码的问题,并且由这个问题,也引出了一个更有意思的问题,笔者也还没有找到这个问题的答案.也希望各位园友指点指点. 二.Java字符编码 ...
- spring-stutrs求解答
这里贴上applicationContext里的代码: <?xml version="1.0" encoding="UTF-8"?> <bea ...
- JavaScript Bind()趣味解答 包懂~~
首先声明一下,这个解答是从Segmentfault看到的,挺有意思就记录下来.我放到最下面: bind() https://developer.mozilla.org/zh-CN/docs/Web/J ...
- CMMI4级实践中的5个经典问题及解答
这五个问题相当经典而且比较深,需要做过CMMI4.5级的朋友才能看懂这些问题.这5个问题是一位正在实践CMMI4级的朋友提出来的,而解答则是我的个人见解. 五个疑问是: A.流程,子流程部分不明白 ...
- 海边直播目标2017全国初中数学竞赛班课堂测试题解答-The Final
1. 设函数 $f(x) = 2^x(ax^2 + bx + c)$ 满足等式 $f(x+1) - f(x) = 2^x\cdot x^2$, 求 $f(1)$. 解答: 由 $f(x) = 2^x( ...
- 知乎大牛的关于JS解答
很多疑惑一扫而空.... http://www.zhihu.com/question/35905242?sort=created JS的单线程,浏览器的多进程,与CPU,OS的对位. 互联网移动的起起 ...
- [问题2014A01] 解答一(第一列拆分法,由张钧瑞同学提供)
[问题2014A01] 解答一(第一列拆分法,由张钧瑞同学提供) (1) 当 \(a=0\) 时,这是高代书复习题一第 33 题,可用升阶法和 Vander Monde 行列式来求解,其结果为 \[ ...
- [问题2014A01] 解答二(后 n-1 列拆分法,由郭昱君同学提供)
[问题2014A01] 解答二(后 n-1 列拆分法,由郭昱君同学提供) \[|A|=\begin{vmatrix} 1 & x_1^2-ax_1 & x_1^3-ax_1^2 &am ...
随机推荐
- list 操作
animals = ["aardvark", "badger", "duck", "emu", "fennec ...
- WPF - 如何引用external dll中图片
前几天一直想引用别的DLL里面的一个图片.总是出现各种各样问题,一气之下将图片拷贝到当前Project中,运行良好.虽然知道引用图片,就1.2.列出来的2点就够了. 1. The Build Acti ...
- java.sql.SQLException:指定了无效的 Oracle URL
java.sql.SQLException:指定了无效的 Oracle URL 昨天晚上用MyEclipse连接Oracle,出现了" java.sql.SQLException: 指定了无 ...
- PHP设计模式笔记八:原型模式 -- Rango韩老师 http://www.imooc.com/learn/236
原型模式 概述: 1.与工厂模式作用类似,都是用来创建对象 2.与工厂模式的实现不同,原型模式是先创建好一个原型对象,然后通过clone原型对象来创建新的对象,这样就免去了类创建时重复的初始化操作 3 ...
- Android静态变量使用陷阱
静态变量大家再熟悉不过了,本来没什么好重复的.事情起因是这样的,最近测试那边反应正在做的一个产品总是莫名其妙的显示不出某些数据,甚至闪退崩溃,仔细查了几遍发现没什么问题,最后百般周折发现在那部测试机上 ...
- 2014牡丹江——Domination
题目链接 题意: 给一个n*m的矩阵,每天随机的在未放棋子的格子上放一个棋子.求每行至少有一个棋子,每列至少有一个棋子的天数的期望 (1 <= N, M <= 50). 分析: 比較明显 ...
- 利用JConsole工具监控java程序内存和JVM
一.找到java应用程序对应的进程PI 性能测试应用程序访问地址:http://192.168.29.218:7070/training/ 部署的应用服务器为tomcat6.028 启动tomcat服 ...
- Sybase自增字段跳号的解决方法
Sybase自增字段跳号原因及影响: 在Sybase数据库中如果数据库在开启的情况下,因为非正常的原因(死机.断电)而导致数据库服务进程强制结束. 那么自动增长的字段将会产生跳号的情况,再往数据表里面 ...
- RMAN的show,list,crosscheck,delete命令
1.SHOW命令: 显示rman配置: RMAN> show all; 2.REPORT命令: 2.1.RMAN> report schema 报告目标数据库的物理结构; 2.2 ...
- python 安装 memcache
方式一: python3 -m pip install python-memcached 方式二: pip3 install python-memcached 方式三: tar zxf python- ...