# 289. Game of Life
###### tags: `leetcode`,`medium`
>ref: https://leetcode.com/problems/game-of-life
>
According to 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."
The board is made up of an m x n grid of cells, where each cell has an initial state: live (represented by a 1) or dead (represented by a 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.
The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously. Given the current state of the m x n grid board, return the next state.



漂亮解法:
1. 結果只用到1,0 ,可使用其他數字代表意義ex. -1 ,2
```java=
class Solution {
public void gameOfLife(int[][] board) {
int[] neighbors = {0, 1, -1};
int rows = board.length, cols = board[0].length;
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
// for each cell, count the number of live neighbors
int liveNeighbors = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (!(neighbors[i] == 0 && neighbors[j] == 0)) {
int r = row + neighbors[i];
int c = col + neighbors[j];
if (r < rows && r >= 0 && c < cols && c >= 0 && Math.abs(board[r][c]) == 1) {
liveNeighbors += 1;
}
}
}
}
// under-population or over-population (rule 1 and 3)
if (board[row][col] == 1 && (liveNeighbors < 2 || liveNeighbors > 3)) {
// -1 means: live -> dead
board[row][col] = -1;
}
// reproduction
if (board[row][col] == 0 && liveNeighbors == 3) {
// 2 means: dead -> live
board[row][col] = 2;
}
}
}
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
board[row][col] = board[row][col] > 0 ? 1 : 0;
}
}
}
}
```
土法煉鋼 記憶該列前一列數值 但為了記錄會需要[2][n]位置 也需要多loop
```java=
public void gameOfLife(int[][] board) {
int m = board.length;
int n = board[0].length;
int[] preRow = new int[n];
int[] curRow = new int[n];
int[][] dir= new int[][] {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
curRow[j]=board[i][j];
}
for(int j=0;j<n;j++){
int cur=board[i][j];
int total=0;
for(int[] eachDir:dir){
int y=eachDir[0];
int x=eachDir[1];
if( (y+i<m) && (x+j<n) && (y+i>=0) && (x+j>=0)){
if(y==-1){
total+=preRow[x+j];
}else if(y==0){
total+=curRow[x+j];
}else{
total+=board[y+i][x+j];
}
}
}
if(cur==1){
// >=2 <=3 => 1
if(total>=2 && total<=3){
board[i][j]=1;
}
// >3 => 0 // <2 => 0
else{
board[i][j]=0;
}
}else{
//==3 => 1
if(total==3){
board[i][j]=1;
}
}
}
for(int k=0;k<n;k++){
preRow[k]= curRow[k];
}
}
}
```