首页 技术 正文
技术 2022年11月20日
0 收藏 893 点赞 4,064 浏览 1960 个字

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.

Follow up:

  1. 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.
  2. 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?

思路:要求 in-place ,所以要区分三种变化状态 1->0,1->1,0->1,将三种状态分别用-1/-2/-3三个状态值来表示

  第一次遍历时,先判断当前cell的转换状态,且将cell值置为对应状态值,在后面的遍历中根据状态值则可推断出原始值,进而判断转换状态。

  第二次遍历,则将状态值置为变换后的值,注意一下在每个cell遍历其周围八个点时用的循环,是怎么排除边界情况的

public class S289 {
public void gameOfLife(int[][] board) {
int m = board.length,n = board[0].length;
for (int i = 0;i < m;i++) {
for (int j = 0;j < n;j++) {
int liveCount = 0;
for (int k = i-1;k <= i+1;k++) {
for (int l = j-1;l <= j+1;l++) {
if(k < 0 || l < 0 || k > m-1 || l > n-1 || (k == i && l == j)) continue;
liveCount += getRealNum(board[k][l]);
}
}
if (board[i][j] == 1) {
if (liveCount <2 || liveCount >3 ) {
board[i][j] = -1;
} else {
board[i][j] = -2;
}
} else {
if (liveCount == 3) {
board[i][j] = -3;
}
}
}
}
for (int i = 0;i < m;i++) {
for (int j = 0;j < n;j++) {
if (board[i][j] == -2 || board[i][j] == -3)
board[i][j] = 1;
else if (board[i][j] == -1)
board[i][j] = 0;
}
}
}
public static int getRealNum(int i){
if(i == -1 || i == -2)
return 1;
else if (i == -3) {
return 0;
}
return i;
}
public static void main(String[] args) {
S289 s = new S289();
int [][]b = {{1}};
s.gameOfLife(b);
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,965
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,486
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,331
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,114
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,747
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,781