Topic:
According to the Wikipedia's article: "The Game of Life, also known simply as Life, was a cellular automaton devised by the British mathematician John Horton Conway in 1970. "
Given a board with m - n cells, each cell have an initial state live (1) or dead (0). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from t He above Wikipedia article):
- Any live cell with fewer than-live neighbors dies, as if caused by under-population.
- Any live cell with a 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 cells 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 it current state.
Follow up:
- Could You solve it in-place? Remember the board needs to be updated at the same time:you cannot update some cells first and then use their update D values to update the other cells.
- In this question, we represent the board using a 2D array. In principle, the board was infinite, which would cause problems when the active area encroaches the border of the array. How would address these problems?
Answer:
Survival Game rules:
MXN, 1 stands for survival, 0 represents the state of death, while the next state of the cell is related to the current state of the cells around it,
1. If a living cell (equal to 1) has a living cell less than 2, it will die in the next state (0) "1->0"
2. If there are 2 or 3 living cells around a living cell, it still survives in the next state "1->1"
3. If a living cell is more than 3 live cells, in the next state it will die "1->0"
4. If the living cells around a dead cell are 3, in the next state it will be resurrected "0->1"
Ask to solve this problem in situ, Note that all cells are updated at the same time, and cannot update other cells with a cell renewal value .
Ideas:
Before the entire matrix is updated, we cannot lose the current value of each cell, so we can put the current value in the low, the updated value is placed in the high, so the above four cases can be expressed as:
01 "Decimal is 1"
11 "Decimal is 3"
01 "Decimal is 1"
10 "Decimal is 2"
Then we can do some binary calculations, which we can observe:
The above digital &1 can get the current state
>>1 can get the next state
1 classSolution {2 Public:3 voidGameoflife (vector<vector<int>>&Board) {4 intx=board.size ();5 inty=board[0].size ();6 for(intI=0; i<x;i++){7 for(intj=0; j<y;j++){8 intNeighbors =getneighbors (board, I, j);9 if(board[i][j]==1){Ten if(neighbors==2|| neighbors==3){ Oneboard[i][j]=3; A } - } - Else if(board[i][j]==0){ the if(neighbors==3){ -board[i][j]=2; - } - } + } - } + for(intI=0; i<x;++i) { A for(intj=0; j<y;++K) { atboard[i][j]>>=1; - } - } - } - intGetneighbors (vector<vector<int>>& Board,intMintN) { - intC=0; in for(inti=m-1; i<=m+1; i++){ - for(intj=n-1; j<=n+1; j + +){ to if(i>=0&& i<board.size () && j>=0&& j<board[0].size ()) { +c + = board[i][j]&1; - } the } * } $c-=board[m][n]&1;Panax Notoginseng returnC; - } the};
289. Game of Life