LeetCode-289. Game of Life

According to Baidu Encyclopedia, the game of life, referred to as life for short, is a cellular automaton invented by British mathematician John Horton Conway in 1970.

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

If the number of viable cells in the eight locations around the viable cell is less than two, the viable cell in that location is dead;
if there are two or three viable cells in the eight locations around the viable cell, the viable cell in that location is still alive;
if the viable cell If there are more than three living cells in the eight surrounding locations, the living cells at that location will die;
if there are exactly three living cells around the dead cells, the dead cells at that location will be resurrected;
according to the current state, write a function to calculate the total number of cells on the panel. A state (after an update). The next state is formed by applying the above rules to each cell in the current state at the same time, where the birth and death of the cells occur simultaneously.

Examples:

Enter:

[
  [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]
]
 

answer:

Since the birth and death of cells occur at the same time, we must first know the state of all cells in the next stage before modifying the array. That is, the original array cannot be modified, and a new array needs to be created to record the state of each cell in the next stage. Create an array as large as the original array to record the status of each grid cell. After traversing the original array, the value of the new array is traversed and assigned to the original array to complete the status update.

Note that the final assignment of the array must be traversal assignment or assignment using the method of the Arrays class. It is not possible to directly make the original array equal to the new array. Because the array in the incoming method is actually a copy of the original array, it has the same address as the original array. We can modify the contents of the address, that is, traverse and assign, but we cannot directly modify the incoming worth-worthy address. After the modification, the original array has nothing to do with the incoming array.

class Solution {
     public void gameOfLife(int[][] board) {
        int m=board.length;
        int n=board[0].length;
        //方向数组
        int[][] direction=new int[][]{{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}};
        //标记数组
        int[][] book=new int[m][n];
        //遍历数组
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                //统计周围活细胞个数
                int count=0;
                //判断每一个细胞周围的情况
                for(int k=0;k<direction.length;k++){
                    int tx=i+direction[k][0];
                    int ty=j+direction[k][1];
                    if(tx>=0&&ty>=0&&tx<m&&ty<n){
                        if(board[tx][ty]==1){
                            count++;
                        }
                    }
                }
                // System.out.println(i+" "+j+"------>"+count);
                if(board[i][j]==0&&count==3){
                    book[i][j]=1;
                }
                if(board[i][j]==1){
                    if(count<2){
                        book[i][j]=0;
                    }
                    if(count==2||count==3){
                        book[i][j]=1;
                    }
                    if(count>3){
                        book[i][j]=0;
                    }
                }
            }
        }
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                board[i][j]=book[i][j];
            }
        }
     }
}
Published 35 original articles · praised 7 · 40,000+ views

Guess you like

Origin blog.csdn.net/weixin_44804750/article/details/105458026