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. 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的更多相关文章

  1. 四种比较简单的图像显著性区域特征提取方法原理及实现-----> AC/HC/LC/FT。

    laviewpbt  2014.8.4 编辑 Email:laviewpbt@sina.com   QQ:33184777 最近闲来蛋痛,看了一些显著性检测的文章,只是简单的看看,并没有深入的研究,以 ...

  2. “LC.exe”错误

    错误“LC.exe”已退出,代码为 -1. 可能的原因是: 这个第三方组件是个商业组件,他在组件的主使用类定义了 LicenseProvider(typeof(LicFileLicenseProvid ...

  3. 解决VS下“LC.exe已退出,代码为-1”问题

    今天使用VS2015开发一个Winform程序,手一抖拖错了一个第三方控件,然后将其去掉并删除相关的引用,结果导致了LC.exe错误:"Lc.exe已退出,代码为-1 ". 经过上 ...

  4. 解析.NET 许可证编译器 (Lc.exe) 的原理与源代码剖析

    许可证编译器 (Lc.exe) 的作用是读取包含授权信息的文本文件,并产生一个可作为资源嵌入到公用语言运行库可执行文件中的 .licenses 文件. 在使用第三方类库时,经常会看到它自带的演示程序中 ...

  5. Lc.exe已退出,代码为-1

    编译项目,出现提示"Lc.exe已退出,代码为-1" .   解决办法: 意思就是把licenses.licx这个文件里的内容删除,但是文件还在(此时是个空文件),发生这个问题的原 ...

  6. "LC.exe" exited with code -1 错误

    当打开一个VS程序时出现"LC.exe" exited with code -1错误,解决方法是: 删除licenses.licx文件即可

  7. LC.exe exited with code -1

    昨天从win8.1升级到win10之后, 一切还算顺利, 就是升级时间比较长. 但是快下班的时候 遇到一个问题, 是之前在win8.1上没遇到的, 首先代码win8.1 vs2013 上跑的时候一切正 ...

  8. vs2012编译出错“LC.exe”已退出解决方法

    “LC.exe”已退出,代码为 -1. 解决方法: 将项目Properties下的licenses.licx文件删除,重新编译即可.

  9. TT付款方式、前TT和后TT、LC信用证+TT付款方式

    TT付款方式是以外汇现金方式结算,由您的客户将款项汇至贵公司指定的外汇银行账号内,可以要求货到后一定期限内汇款. .T/T属于商业信用,也就是说付款的最终决定权在于客户.T/T分预付,即期和远期.现在 ...

随机推荐

  1. php和js的小区别

    1.今天看了下php的api感觉还可以,不是很难,可能没看到深入的地方, (1)和js很相似 目前感觉它和js的最大区别 js的  点  被替换成 -> function setCate($pa ...

  2. PAT 2012 冬

    A Shortest Distance 题意相当于一个环,找出两点间从不同方向得到的距离中的最小值. #include <cstdio> #include <iostream> ...

  3. Java中String类为什么被设计为final?

    Java中String类为什么被设计为final   首先,String是引用类型,也就是每个字符串都是一个String实例.通过源码可以看到String底层维护了一个byte数组:private f ...

  4. Cracking Digital VLSI Verification Interview 第一章

    目录 Digital Logic Design Number Systems, Arithmetic and Codes Basic Gates Combinational Logic Circuit ...

  5. Java学习十五

    学习内容: MyBaits 以前从来没有接触过mybatis,通过今天的学习知道这是一个框架,适用于关注SQL优化和需要频繁更新的项目. 今天做一个关于mybatis项目的入门小程序,效果很不理想. ...

  6. dfs--迷宫

    题目背景 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫中移动有上下左右四种方式,每次只能移 ...

  7. 瑞芯微RK3399六核-迅为3399开发板介绍

    迅为3399开发板基于瑞芯微的RK3399处理器设计,Rockchip RK3399是瑞芯微推出的一款低功耗.高性能的应用处理器芯片,该芯片基于Big.Little架构,即具有独立的NEON协同处理器 ...

  8. 生产事故(MongoDB数据分布不均解决方案)

    可以很明显可以看到我们这个集合的数据严重分布不均匀. 一共有8个分片,面对这个情况我首先想到的是手动拆分数据块,但这不是解决此问题的根本办法. 造成此次生产事故的首要原因就是片键选择上的问题,由于片键 ...

  9. Sublime Text3 作Markdown编辑器 配置

    准备 Sublime 配置 1 Package Control 2 安装插件 测试 其他 1. 准备 Windows操作系统 Sublime Text3 编辑器 Web浏览器 2. Sublime 配 ...

  10. ZJNU 1160 - 不要62——中级

    取模判断,数组模拟 /* Written By StelaYuri */ #include<stdio.h> ]; int main(){ int n,m,i,s,t; ;i<;i+ ...