[LC] 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. 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]
]
class Solution {
/***
0, 1, current dead, live
2nd bit means next live
00 current dead -> next dead
01 current live -> next dead
10 current Dead -> next live
11 current live -> next live
*/
public void gameOfLife(int[][] board) {
if (board == null || board.length == 0 || board[0].length == 0) {
return;
}
int row = board.length;
int col = board[0].length;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
int count = countCell(board, i, j);
if (board[i][j] == 1) {
// for live, case 2
if (count == 2 || count == 3) {
// left shift
board[i][j] += 2;
}
} // for live, case4
else if (count == 3) {
board[i][j] += 2;
}
}
}
// right shift to first bit status
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
board[i][j] = board[i][j] >> 1;
}
}
}
private int countCell(int[][] board, int m, int n) {
int count = 0;
// need to include ==, otherwise cannot reach m+ 1/ n + 1
for (int i = Math.max(m - 1, 0); i <= Math.min(m + 1, board.length - 1); i++) {
for (int j = Math.max(n - 1, 0); j<= Math.min(n + 1, board[0].length - 1); j++) {
if (i == m && j == n) {
continue;
}
if ((board[i][j] & 1) == 1) {
count += 1;
}
}
}
return count;
}
}
[LC] 289. Game of Life的更多相关文章
- 四种比较简单的图像显著性区域特征提取方法原理及实现-----> AC/HC/LC/FT。
laviewpbt 2014.8.4 编辑 Email:laviewpbt@sina.com QQ:33184777 最近闲来蛋痛,看了一些显著性检测的文章,只是简单的看看,并没有深入的研究,以 ...
- “LC.exe”错误
错误“LC.exe”已退出,代码为 -1. 可能的原因是: 这个第三方组件是个商业组件,他在组件的主使用类定义了 LicenseProvider(typeof(LicFileLicenseProvid ...
- 解决VS下“LC.exe已退出,代码为-1”问题
今天使用VS2015开发一个Winform程序,手一抖拖错了一个第三方控件,然后将其去掉并删除相关的引用,结果导致了LC.exe错误:"Lc.exe已退出,代码为-1 ". 经过上 ...
- 解析.NET 许可证编译器 (Lc.exe) 的原理与源代码剖析
许可证编译器 (Lc.exe) 的作用是读取包含授权信息的文本文件,并产生一个可作为资源嵌入到公用语言运行库可执行文件中的 .licenses 文件. 在使用第三方类库时,经常会看到它自带的演示程序中 ...
- Lc.exe已退出,代码为-1
编译项目,出现提示"Lc.exe已退出,代码为-1" . 解决办法: 意思就是把licenses.licx这个文件里的内容删除,但是文件还在(此时是个空文件),发生这个问题的原 ...
- "LC.exe" exited with code -1 错误
当打开一个VS程序时出现"LC.exe" exited with code -1错误,解决方法是: 删除licenses.licx文件即可
- LC.exe exited with code -1
昨天从win8.1升级到win10之后, 一切还算顺利, 就是升级时间比较长. 但是快下班的时候 遇到一个问题, 是之前在win8.1上没遇到的, 首先代码win8.1 vs2013 上跑的时候一切正 ...
- vs2012编译出错“LC.exe”已退出解决方法
“LC.exe”已退出,代码为 -1. 解决方法: 将项目Properties下的licenses.licx文件删除,重新编译即可.
- TT付款方式、前TT和后TT、LC信用证+TT付款方式
TT付款方式是以外汇现金方式结算,由您的客户将款项汇至贵公司指定的外汇银行账号内,可以要求货到后一定期限内汇款. .T/T属于商业信用,也就是说付款的最终决定权在于客户.T/T分预付,即期和远期.现在 ...
随机推荐
- promise核心 为什么用promise
为什么要用promise 1.使用纯回调函数 先指定回调函数,再启动异步任务 答 1.指定回调函数的方式更加灵活 可以在执行任务前,中,后 2.支持链式调用,解决回调地狱问题 什么是回调地狱:回调函数 ...
- CountDownLatch和CyclicBarrier和Semaphore最通俗形象解释
应该还有好多同学对这三个的区别比较模糊,网络上其他文章说的也比较专业化.所以我在这里举个例子说明这三个的区别. 我们假定有一场百米比赛,比赛包括十个运动员和一个裁判,每个运动员和每个裁判都是一个线程, ...
- python counter、闭包、generator、解数学方程、异常
1.counter 2.闭包 3.generator 4.解数学方程 5.异常 1.python库——counter from collections import Counter breakfast ...
- JZOJ-TGB817-SOL
T1 题面 "封印大典启动,请出Nescafe魂珠!"随着圣主applepi一声令下,圣剑护法rainbow和魔杖护法freda将Nescafe魂珠放置于封印台上.封印台是一个树形 ...
- HashMap面试总结
作者:孤独烟 出处: http://rjzheng.cnblogs.com/ 文章由点及线再及面,写的非常好.修改部分内容 (1) HashMap的实现原理 看过HashMap源码吗,知道原理吗? h ...
- js数组,字符转换;key_value获取
js 数组转字符串js 字符串转数组arr.join(",");String.split(","); 对象的所有keys Object.keys(val)对象的 ...
- C基础 带你手写 redis ae 事件驱动模型
引言 - 整体认识 redis ae 事件驱动模型, 网上聊得很多. 但当你仔细看完一篇又一篇之后, 可能你看的很舒服, 但对于 作者为什么要这么写, 出发点, 好处, 缺点 ... 可能还是好模糊, ...
- 个人安装GO1.13.6版本指南手册之搭建环境
因好奇而走进go语言,让你不在只闻其声,不见其形. https://golang.org/doc/install:这里是go语言的官网文档.吃不透英文,终究会被限制在有限的区域,一词词的吃透. 安装包 ...
- java.io.tmpdir在哪里?
查找所在目录的方式如下: System.out.println(System.getProperty(“java.io.tmpdir”)); System.getProperty(),还可以获取更多其 ...
- update-help : 无法更新带有 UI 区域性 {zh-CN} 的模块“WindowsUpdateProvider”帮助: 在 HelpInfo XML 文件中检索不到 UI 区域性 zh-CN
环境 OS: Windows10 企业版 LTSC x64 CPU: Intel i5-7500 CPU 3.4GHz PowerShell:5.1.17763.503 描述 更新powershell ...