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?
Links: http://leetcode.com/problems/game-of-life/
Exercises
Life games. The topic is relatively long, with extra array to do is relatively simple, but requires in-place words, we need to use some skills. The method here comes from Yavinci, and many of his Java solutions are both subtle and easy to read, and really powerful. We use two bit bits to represent the board of the current and next rounds.
00 represents the current dead
01 stands for current live, next dead
10 represents the current Dead,next live
11 represents current and Next are live
Follow test instructions to update the current cell, after all the update, you need to traverse the entire array again, the board upgrade to the next round, that is, each cell >>= 1.
Time Complexity-o (MN), Space complexity-o (1).
Public classSolution { Public voidGameoflife (int[] board) { if(board = =NULL|| Board.length = = 0) { return; } for(inti = 0; i < board.length; i++) { for(intj = 0; J < Board[0].length; J + +) { intLiveneighbors =getliveneighbors (board, I, J); if((Board[i][j] & 1) = = 1) { if(liveneighbors >= 2 && liveneighbors <= 3) {Board[i][j]= 3;//Change to "one", still live}//else it stays as "which," would be eleminated next upgrade}Else { if(Liveneighbors = = 3) {Board[i][j]= 2;//Change to "Ten", become live } } } } for(inti = 0; i < board.length; i++) { for(intj = 0; J < Board[0].length; J + +) {Board[i][j]>>= 1; } } } Private intGetliveneighbors (int[] board,intRowintCol) { intres = 0; for(inti = Math.max (row-1, 0); I <= math.min (board.length-1, row + 1); i++) { for(intj = Math.max (col-1, 0); J <= Math.min (board[0].length-1, col + 1); J + +) {res+ = Board[i][j] & 1; }} Res-= Board[row][col] & 1; returnRes; }}
Reference:
Https://leetcode.com/discuss/68352/easiest-java-solution-with-explanation
Https://leetcode.com/discuss/61912/c-o-1-space-o-mn-time
Https://leetcode.com/discuss/61910/clean-o-1-space-o-mn-time-java-solution
289. Game of Life