[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分预付,即期和远期.现在 ...
随机推荐
- 《Thinking in Java》中讲到了final,发现自己有部分地方迷糊
1.1当给全局的静态字段加上final时,系统是不会给其赋默认值的,若不手动初始化,会编译时错误——Variable 'xxx' might not have been initialized. 1. ...
- Java底层魔术类Unsafe用法简述
1 引子 Java中没有指针,不能直接对内存地址的变量进行控制,但Java提供了一个特殊的类Unsafe工具类来间接实现.Unsafe主要提供一些用于执行低级别.不安全操作的方法,如直接访问系统内存资 ...
- redis常用命令--zsets
zsets常用命令: zadd key score1 mb1 [score2 mb2....]:像key中添加元素和这个元素的分数,如果元素已经存在,则替换分数. zscore key mb :获取k ...
- git登录账号密码错误remote: Incorrect username or password (access token)
git提交时弹框让输入用户和密码,不小心输入错误了 再次提交 一直就提示 remote: Incorrect username or password 错误了,也不弹框要重新输入 解决方法 win1 ...
- JavaScript 之 异步请求
一. 1.异步(async) 异步,它的孪生兄弟--同步(Synchronous),"同步模式"就是上一段的模式,后一个任务等待前一个任务结束,然后再执行,程序的执行顺序与任务的排 ...
- 关于阿里云的远程连接和轻型桌面(xfce4)安装
这里用的阿里云服务器是轻量应用服务器 先通过网页端的远程连接进入服务器,然后 安装xfce4 (1)先安装更新:apt-get update. (2)安装xrdp:输入apt-get install ...
- dp--最长上升子序列LIS
1759:最长上升子序列 总时间限制: 2000ms 内存限制: 65536kB 描述 一个数的序列bi,当b1 < b2 < ... < bS的时候,我们称这个序列是上升的.对 ...
- Tensorflow函数——tf.set_random_seed(seed)
设置图级随机seed. 依赖于随机seed的操作实际上从两个seed中获取:图级和操作级seed. 这将设置图级别的seed. 其与操作级seed的相互作用如下: 1.如果没有设置图形级别和操作see ...
- 计蒜客 密码锁(BFS)
https://www.jisuanke.com/course/1797/121114 Description 现在一个紧急的任务是打开一个密码锁.密码由四位数字组成,每个数字从 1 到 9 进行编号 ...
- Servlet&JSP复习笔记 03
1.Servlet的声明周期 容器如何创建Servlet对象,如何为Servlet对象分配资源,如何调用Servlet对象的方法来处理请求,以及如何销毁Servlet对象的过程. a.实例化 容器调用 ...