# 289. Game of Life

According to the [Wikipedia's article](https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life): "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](https://en.wikipedia.org/wiki/Moore_neighborhood) (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]
]
```

**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?

```java
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

{% embed url="<https://segmentfault.com/a/1190000003819277>" %}

把活细胞要死去的标为2，把死细胞要活得标为3，最后每个都取余2

```java
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;
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://shuati.gitbook.io/crack-lintcode/arrays/289.-game-of-life.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
