According to the : "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 (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.
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.
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?
class Solution {
public void gameOfLife(int[][] board) {
//1 邻居小于2个活着的,就死
//2 邻居2个或3个活着,活着
//3 3个以上活着 死
//4 死细胞 三个邻居或者 活
if(board == null || board.length == 0 || board[0].length == 0) return;
int[][] res = new int[board.length][board[0].length];
for(int i = 0; i < board.length; i++){
for(int j = 0; j < board[0].length;j++){
//live
if(help(i,j,board)){
res[i][j] = 1;
}else{
res[i][j] = 0;
}
}
}
for(int i = 0; i < board.length;i++){
for(int j = 0; j < board[0].length; j++){
board[i][j] = res[i][j];
}
}
return ;
}
public boolean help(int i, int j, int[][] board){
if(board[i][j] == 1){
//check neibor
int live = getLive(i,j,board);
if(live < 2) return false;
else if(live <= 3) return true;
else return false;
}else{
int live = getLive(i,j,board);
if(live == 3) return true;
else return false;
}
}
public int getLive(int i,int j,int[][] board){
int live = 0;
//up (i-1,j),down(i+1,j),left(i,j-1),right(i,j+1)
// up letf (i-1,j-1),up right(i-1,j+1),down left (i+1,j-1),down right (i+1,j+1)
int[] x = {1,-1,0,0,-1,1,1,-1};
int[] y = {0,0,1,-1,1,1,-1,-1};
for(int k = 0; k < 8;k++){
int nx = i + x[k];
int ny = j + y[k];
if(nx < 0 || nx >= board.length || ny < 0 || ny >= board[0].length) continue;
if(board[nx][ny] == 1) live++;
}
return live;
}
}
follow up: 1.in place , 2: 2d array infinite
把活细胞要死去的标为2,把死细胞要活得标为3,最后每个都取余2
class Solution {
public void gameOfLife(int[][] board) {
//1 邻居小于2个活着的,就死
//2 邻居2个或3个活着,活着
//3 3个以上活着 死
//4 死细胞 三个邻居或者 活
if(board == null || board.length == 0 || board[0].length == 0) return;
int[][] res = new int[board.length][board[0].length];
for(int i = 0; i < board.length; i++){
for(int j = 0; j < board[0].length;j++){
//live
help(i,j,board);
}
}
for(int i = 0; i < board.length;i++){
for(int j = 0; j < board[0].length; j++){
board[i][j] = board[i][j] % 2;
}
}
}
public void help(int i,int j,int[][] board){
int live = 0;
//up (i-1,j),down(i+1,j),left(i,j-1),right(i,j+1)
// up letf (i-1,j-1),up right(i-1,j+1),down left (i+1,j-1),down right (i+1,j+1)
int[] x = {1,-1,0,0,-1,1,1,-1};
int[] y = {0,0,1,-1,1,1,-1,-1};
for(int k = 0; k < 8;k++){
int nx = i + x[k];
int ny = j + y[k];
if(nx < 0 || nx >= board.length || ny < 0 || ny >= board[0].length) continue;
if(board[nx][ny] == 1 || board[nx][ny] == 2) live++;
}
if(board[i][j]== 1 && (live > 3 || live < 2)) board[i][j] = 2;
if(board[i][j] == 0 && live == 3) board[i][j] = 3;
}
}