[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分预付,即期和远期.现在 ...
随机推荐
- expdp远程导出oracle库
1.手动在本地建目录 E:\lvchengData 2.执行命令 create or replace directory data as 'E:\lvchengData\'; 3.为本地system用 ...
- <kotlin>基础,杂七杂八(亲测有效)
okhttp class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) ...
- openstack trove weekly meeting时间即将更改
为了平衡英国.巴黎.德国.美国和中国开发者的作息习惯,openstack trove项目组在5月18日的weekly meeting上开始讨论新的开会时间. 当前的开会时间是,周三 UTC 18:00 ...
- CSS样式实现两个图片平分三角
<div class='pageOption'> <a href='#' class='option' > <img src='http://imgsrc.hubbles ...
- viewer.js插件简单使用说明
不需要依赖jQuery.js,只需要导入viewer.js和viewer.css文件即可. 插件GitHub地址:https://github.com/fengyuanchen/viewerjs 示例 ...
- offset系列、client系列、scroll系列
offset系列.client系列 <style> .testDOM { width: 200px; height: 200px; background-color: #2de; pa ...
- ACwing算法基础课听课笔记(第一章,基础算法一)(二分)
二分法: 在看这个视频前,我对于二分法是一头雾水的,又加上这个算法平常从来没写过所以打了一年了还没正式搞过.视频提到ACwing上的一道题,我用自以为聪明的方法去做,结果TLE了,实在丢人,不说了,开 ...
- c++静态库和动态库的添加
# 声明要求的 cmake 最低版本cmake_minimum_required(VERSION 2.8)# 声明一个 cmake 工程project(helloSLAM) # 设置编译模式set( ...
- mysql my.cnf常用的配置
[mysqld]basedir = /usr/local/mysql/datadir = /usr/local/mysql/dataport = 3306pid-file = /usr/local/m ...
- Asp.Net MVC主项目访问不到分离项目控制器的解决方案
我在portal主项目外新建一个分离项目,控制器和Model都写在分离项目中,视图层写在portal中. 我更改了命名空间,引用了Dll,还是不能访问到控制器. 找到问题: 最后我发现是主项目port ...