#pattern
actual    update      ref 
0             0        0
1             1        1
0             1       -1
1             0       -2

class Solution:
	def gameOfLife(self, board: List[List[int]]) -> None:
		r = len(board)
		c = len(board[0])
		ans = [[0]*c for _ in range(r)]
		neighs = [[1,0],[-1,0],[0,1],[0,-1],[-1,-1],[-1,1],[1,1],[1,-1]]

		for i in range(r):
			for j in range(c):
				livecnt,deadcnt = 0,0
				for di,dj in neighs:
					if 0<=(i+di) < r and 0<=(j+dj)<c:
						if board[i+di][j+dj] == 0 or board[i+di][j+dj] == -1 :
							deadcnt+=1
						else:
							livecnt+=1        
				if board[i][j] == 0:
					if livecnt == 3:
						board[i][j] = -1
				else:
					if livecnt == 2 or livecnt==3:
						board[i][j] = 1
					else:
						board[i][j] = -2
		for i in range(r):
			for j in range(c):
				if board[i][j] == -1:
					board[i][j] = 1
				elif board[i][j] == -2:
					board[i][j] = 0
class Solution {
    public void gameOfLife(int[][] board) {
        int m = board.length, n = board[0].length;
        int[][] next = new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                next[i][j] = nextState(board, i, j, m, n);
            }
        }
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                board[i][j] = next[i][j];
            }
        }
    }

    public int nextState(int[][] board, int i, int j, int m, int n) {
        int ones = 0;
        for (int x = -1; x <=1; x++) {
            for (int y = -1; y <= 1; y++) {
                if (x == 0 && y == 0) {
                    continue;
                }
                int a = i + x, b = j + y;
                if (a >= 0 && a < m) {
                    if (b >= 0 && b < n) {
                        ones += board[a][b];
                    }
                }
            }
        }
        if (board[i][j] == 0) {
            return ones == 3 ? 1 : 0;
        } else {
            if (ones == 2 || ones == 3) {
                return 1;
            } else {
                return 0;
            }
        }
    }
}
Game of Life
According to&nbsp;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.

&nbsp;
Example 1:

Input: board = [[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]]


Example 2:

Input: board = [[1,1],[1,0]]
Output: [[1,1],[1,1]]


&nbsp;
Constraints:


	m == board.length
	n == board[i].length
	1 &lt;= m, n &lt;= 25
	board[i][j] is 0 or 1.


&nbsp;
Follow up:


	Could you solve it in-place? Remember that the board needs to be updated simultaneously: 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 upon the border of the array (i.e., live cells reach the border). How would you address these problems?


/**
 * @param {number[][]} board
 * @return {void} Do not return anything, modify board in-place instead.
 */
var gameOfLife = function(board) {
    const m = board.length, n = board[0].length;
    let copy = JSON.parse(JSON.stringify(board));

    const getNeighbor = (row, col) => {
        let radius = [-1, 0, 1], count = 0;
        for(let i = 0; i < 3; i++) {
            for(let j = 0; j < 3; j++) {
                if(!(radius[i] == 0 && radius[j] == 0) && copy[row + radius[i]] && copy[row + radius[i]][col + radius[j]]) {
                    let neighbor = copy[row + radius[i]][col + radius[j]];
                    if(neighbor == 1) {
                        count++;
                    }
                }
            }
        }
        return count;
    }

    for(let i = 0; i < m; i++) {
        for(let j = 0; j < n; j++) {
            const count = getNeighbor(i, j);
            if(copy[i][j] == 1) {
                if(count < 2 || count > 3) {
                    board[i][j] = 0;
                }
            } else {
                if(count == 3) {
                    board[i][j] = 1;
                }
            }
        }
    }
};
// Idea: Encode the value into 2-bit value, the first bit is the value of next state, and the second bit is the value of current state
class Solution {
public:
    void gameOfLife(vector<vector<int>>& board) {
        int m = board.size();
        int n = board[0].size();
        for (int i=0; i<m; ++i) {
            for (int j=0; j<n; ++j) {
                encode(board, i, j);
            }
        }
        for (int i=0; i<m; ++i) {
            for (int j=0; j<n; ++j) {
                board[i][j] >>= 1;
            }
        }
        
    }
    void encode(vector<vector<int>>& board, int row, int col) {
        int ones = 0;
        int zeros = 0;
        int m = board.size();
        int n = board[0].size();
        int cur = board[row][col];
        if (row >= 1 && col >= 1) {
            ones += (board[row - 1][col - 1] & 1);
            zeros += !(board[row - 1][col - 1] & 1);
        }
        if (row >= 1) {
            ones += (board[row - 1][col] & 1);
            zeros += !(board[row - 1][col] & 1);
        }
        if (row >= 1 && col < n - 1) {
            ones += (board[row - 1][col + 1] & 1);
            zeros += !(board[row - 1][col + 1] & 1);
        }
        if (col < n - 1) {
            ones += (board[row][col + 1] & 1);
            zeros += !(board[row][col + 1] & 1);
        }
        if (row < m - 1 && col < n - 1) {
            ones += (board[row + 1][col + 1] & 1);
            zeros += !(board[row + 1][col + 1] & 1);
        }
        if (row < m - 1) {
            ones += (board[row + 1][col] & 1);
            zeros += !(board[row + 1][col] & 1);
        }
        if (row < m - 1 && col >= 1) {
            ones += (board[row + 1][col - 1] & 1);
            zeros += !(board[row + 1][col - 1] & 1);
        }
        if (col >= 1) {
            ones += (board[row][col - 1] & 1);
            zeros += !(board[row][col - 1] & 1);
        }
        if (ones < 2 && cur == 1) {
            cur += 0 << 1;
        } else if (ones >= 2 && ones <= 3 && cur == 1) {
            cur += 1 << 1;
        } else if (ones > 3 && cur == 1) {
            cur += 0 << 1;
        } else if (ones == 3 && cur == 0) {
            cur += 1 << 1;
        } else {
            cur += cur << 1;
        }
        board[row][col] = cur;
    }
};