# [caption] life game – leetcode

Time：2021-2-16

## Title: Life Game

According to Baidu Encyclopedia, life game, referred to as life, is a cellular automata invented by British mathematician John Houghton Conway in 1970.

Given a panel containing m × n lattices, each lattice can be regarded as a cell. Each cell has an initial state: 1 is live cell, or 0 is dead cell. Each cell and its eight adjacent cells (horizontal, vertical, diagonal) follow the following four survival laws:

1. If there are less than two living cells in eight locations around the living cells, the living cells in that location will die;
2. If there are two or three living cells in eight locations around the living cells, the living cells in that location are still alive;
3. If there are more than three living cells in eight locations around the living cells, the living cells in that location will die;
4. If there are exactly three living cells around the dead cells, the dead cells will revive;

According to the current state, write a function to calculate the next (Updated) state of all cells on the panel. The next state is formed by applying the above rules to each cell in the current state at the same time, in which the birth and death of cells 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]
]

Can you use the in place algorithm to solve this problem?

Please note that all grids on the panel need to be updated at the same time: you cannot update some grids first, and then update others with their updated values.
In this problem, we use two-dimensional array to represent the panel. In principle, the panel is infinite, but it can cause problems when living cells invade the boundary of the panel. How will you solve these problems?

The difficulty of this problem is that if you directly update the original array, it will affect the cells in other locations. The problem requires that you update the original array at the same time.

We can copy a template, which is used to record the number of cells in the adjacent lattice of each position in this state.

Through this template, we can modify the original array and meet the requirements of updating at the same time.

Code (c) above:

``````void gameOfLife(int** board, int boardSize, int* boardColSize){

Int f [8] [2] = {- 1,0}, {- 1,1}, {0,1}, {1,1}, {1,0}, {1, - 1}, {0, - 1}, {- 1, - 1}; // eight directions
int hang = boardSize;
int lie = *boardColSize;

int tmp[hang+1][lie+1];
Memset (TMP, 0, sizeof (TMP)); // count the total number of cells around
for ( int i = 0; i <= hang; i++ )
for ( int j = 0; j <= lie; j++ ) {
for( int z = 0; z < 8; z++ ) {
int x = i + f[z][0];
int y = j + f[z][1];
if( x >= 0&& x < hang&& y >= 0&& y < lie&& board[x][y] == 1 ) tmp[i][j]++;
}
}// modify the original array
for( int i = 0; i < hang; i++ )
for( int j = 0; j < lie; j++ ) {
if( board[i][j] == 1 ) {
if( tmp[i][j] < 2|| tmp[i][j] > 3 ) board[i][j] = 0;
}
else {
if( tmp[i][j] == 3 ) board[i][j] = 1;
}
}
}``````

2020-04-0214:30:15

## [Q & A share the second bullet] MySQL search engine, after watching the tyrannical interviewer!

Hello, I’m younger brother. A few days ago, I shared the second interview question, the interview site of search engine in MySQL. This question is the interview at normal temperature. After reading it, I’m sure you will gain something in terms of database engine If you haven’t read my first share, you can refer to […]