According to the 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."Given a board with m by n cells, each cell has 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 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.Write a function to compute the next state (after one update) of the board given its current state.Follow up: Could you solve it in-place? Remember that the board needs to be updated at the same time: 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 the border of the array. How would you address these problems?
Reference: http://segmentfault.com/a/1190000003819277
Encoding/Decoding Complexity
Time O (NN) Space O (1)
Ideas
The simplest method is to create another matrix to save the data. However, when the inplace solution is used, if we directly modify the current value based on the number of surviving points around each point, since the matrix is traversed sequentially, this will affect the calculation of the next vertex. How can we ensure that the calculation of the next vertex will not be affected while modifying the value? In fact, we only need to encode the value slightly, because the question gives an int matrix, which can be used in a large space. Here we assume that for a certain point, the meaning of the value is
0: the previous round is 0, after this round is still 01: the previous round is 1, after this round is 12: the previous round is 1, after this round is changed to 03: the previous round is 0, after this round, it becomes 1
In this way, for a node, if the surrounding vertex is 1 or 2, it indicates that the node is active in one round. Finally, after traversing the array, return the encoding, that is, if both 0 and 2 are changed back to 0, 1, and 3, all are changed back to 1.
Note:
Note the encoding method. Both 1 and 3 are regarded as 1 after this round, so that a modulo 2 operation can be used to directly decode it.
During implementation, I didn't create an array corresponding to the eight points around in advance, because the actual complexity is the same, adding a few more arrays reduces the readability of the program.
1 public class Solution { 2 public void gameOfLife(int[][] board) { 3 if (board==null || board.length==0 || board[0].length==0) return; 4 int m = board.length; 5 int n = board[0].length; 6 for (int i=0; i<m; i++) { 7 for (int j=0; j<n; j++) { 8 int lives = 0; 9 //up10 if (i > 0) {11 if (board[i-1][j]==1 || board[i-1][j]==2) lives++;12 }13 //down14 if (i < m-1) {15 if (board[i+1][j]==1 || board[i+1][j]==2) lives++;16 }17 //left18 if (j > 0) {19 if (board[i][j-1]==1 || board[i][j-1]==2) lives++;20 }21 //right22 if (j < n-1) {23 if (board[i][j+1]==1 || board[i][j+1]==2) lives++;24 }25 //left up26 if (i>0 && j>0) {27 if (board[i-1][j-1]==1 || board[i-1][j-1]==2) lives++;28 }29 //right up30 if (i>0 && j<n-1) {31 if (board[i-1][j+1]==1 || board[i-1][j+1]==2) lives++;32 }33 //left down34 if (i<m-1 && j>0) {35 if (board[i+1][j-1]==1 || board[i+1][j-1]==2) lives++; 36 }37 //right down38 if (i<m-1 && j<n-1) {39 if (board[i+1][j+1]==1 || board[i+1][j+1]==2) lives++;40 }41 42 43 //update board[i][j]44 if (board[i][j] == 0) {45 if (lives == 3) board[i][j] = 3;46 else board[i][j] = 0;47 }48 else { //board[i][j] == 149 if (lives<2 || lives>3) board[i][j] = 2;50 else board[i][j] = 1;51 }52 }53 }54 55 //decode 56 for (int i=0; i<m; i++) {57 for (int j=0; j<n; j++) {58 board[i][j] %= 2;59 }60 }61 }62 }
Leetcode: Game of Life