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 of the board is determined by applying the above rules simultaneously to every cell in the current state of the m x n grid board. In this process, births and deaths occur simultaneously.
Given the current state of the board, update the board to reflect its next state.
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?
Solutions
Solution 1: In-place marking
Let's define two new states. State $2$ indicates that the living cell becomes dead in the next state, and state $-1$ indicates that the dead cell becomes alive in the next state. Therefore, for the current grid we are traversing, if the grid is greater than $0$, it means that the current grid is a living cell, otherwise it is a dead cell.
So we can traverse the entire board, for each grid, count the number of living neighbors around the grid, and use the variable $live$ to represent it. If the current grid is a living cell, then when $live \lt 2$ or $live \gt 3$, the next state of the current grid is a dead cell, that is, state $2$; if the current grid is a dead cell, then when $live = 3$, the next state of the current grid is an active cell, that is, state $-1$.
Finally, we traverse the board again, and update the grid with state $2$ to a dead cell, and update the grid with state $-1$ to an active cell.
The time complexity is $O(m \times n)$, where $m$ and $n$ are the number of rows and columns of the board, respectively. We need to traverse the entire board. And the space complexity is $O(1)$.
/** Do not return anything, modify board in-place instead. */functiongameOfLife(board:number[][]):void{constm=board.length;constn=board[0].length;for(leti=0;i<m;++i){for(letj=0;j<n;++j){letlive=-board[i][j];for(letx=i-1;x<=i+1;++x){for(lety=j-1;y<=j+1;++y){if(x>=0&&x<m&&y>=0&&y<n&&board[x][y]>0){++live;}}}if(board[i][j]===1&&(live<2||live>3)){board[i][j]=2;}if(board[i][j]===0&&live===3){board[i][j]=-1;}}}for(leti=0;i<m;++i){for(letj=0;j<n;++j){if(board[i][j]===2){board[i][j]=0;}if(board[i][j]===-1){board[i][j]=1;}}}}
constDIR:[(i32,i32);8]=[(-1,0),(1,0),(0,-1),(0,1),(-1,-1),(-1,1),(1,-1),(1,1),];implSolution{#[allow(dead_code)]pubfngame_of_life(board:&mutVec<Vec<i32>>){letn=board.len();letm=board[0].len();letmutweight_vec:Vec<Vec<i32>>=vec![vec![0;m];n];// Initialize the weight vectorforiin0..n{forjin0..m{ifboard[i][j]==0{continue;}for(dx,dy)inDIR{letx=(iasi32)+dx;lety=(jasi32)+dy;ifSelf::check_bounds(x,y,nasi32,masi32){weight_vec[xasusize][yasusize]+=1;}}}}// Update the boardforiin0..n{forjin0..m{ifweight_vec[i][j]<2{board[i][j]=0;}elseifweight_vec[i][j]<=3{ifboard[i][j]==0&&weight_vec[i][j]==3{board[i][j]=1;}}else{board[i][j]=0;}}}}#[allow(dead_code)]fncheck_bounds(i:i32,j:i32,n:i32,m:i32)->bool{i>=0&&i<n&&j>=0&&j<m}}