Topic describes

The description of the topic is too complex, you can jump to see.

Their thinking

In fact, this problem, you see the first glance should know very simple.

Updating at the same time means making a copy of the data for later use,

Update the new cell array based on the original backup data.

  1. boardBak = copy(board)

Then look at the core idea

  1. Count the number of 1s in eight positions
  2. ==3 that must live, ==2 to die, the other situation is fart
boardBak[y-1][x-1] + boardBak[y+1][x+1] + boardBak[y-1][x] + boardBak[y][x-1] + boardBak[y-1][x+1] + boardBak[y+1][x-1] + boardBak[y][x+1] + boardBak[y+1][x]; if (sum === 3) { board[y][x] = 1; } else if(sum ! == 2) { board[y][x] = 0; }Copy the code

That’s the core of so, no difficulty at all.

Then, assuming m is the number of rows and n is the number of columns, wrap the following loop around the core code and it’s ok

for (y=0, y < m) for (x=0, x < n)

Isn’t it easy!!

But unfortunately, the report is wrong. Because there aren’t enough cells on the sides.

So what to do.

I did it like this.

[[0, 0], [0, 1],,1,1 [1], [0, 0]] = = = = > [,0,1,0,0 [0], [0,0,1,0,0], [0,0,0,1,0], [0,1,1,1,0], [0,0,0,0,0]. ,0,0,0,0 [0],]Copy the code

Does it look rough

I used a circle of dead cells (0) to wrap our target tissue, so that both the boundary cells and the inner cells can be treated as 8 peripheral cells

Of course, we don’t modify the target cells directly, but we modify the backup cells.

After that, we just need to change the cycle conditions, so that we can go from the cell at (1,1) to (m-1,n-1), and not go through the boundary of our package.

for (y=1, y < m-1) for (x=1, x < n-1)

Also, the coordinates of our original array and backup array are different now. The backup array has an extra layer, so the position of the backup cells in the original array needs to be after -1. if (sum === 3) { board[y-1][x-1] = 1; } else if(sum ! == 2) { board[y-1][x-1] = 0; }Copy the code

That’s the end of the matter.

Here is the implementation.

code

/ * * *@param {number[][]} board
 * @return {void} Do not return anything, modify board in-place instead.
 */

var gameOfLife = function(board) {
    let boardBak = [];
    let n = board[0].length;
     board.map((row) = >{
        boardBak.push([0. row,0]);
    })
    boardBak = [new Array(n + 2).fill(0), ...boardBak, new Array(n + 2).fill(0)]
    
    let m = boardBak.length;
    n = boardBak[0].length;
    
    for (let y = 1; y < m - 1; y++) {
        for (let x = 1; x < n - 1; x++) {
            let sum = 0;
            sum = boardBak[y-1][x-1] + 
            boardBak[y+1][x+1] + 
            boardBak[y-1][x] + 
            boardBak[y][x-1] + 
            boardBak[y-1][x+1] + 
            boardBak[y+1][x-1] + 
            boardBak[y][x+1] + 
            boardBak[y+1][x];
            if (sum === 3) {
                board[y-1][x-1] = 1;
            } else if(sum ! = =2) {
                board[y-1][x-1] = 0; }}}return board;
};
Copy the code

conclusion


This article is participating in the “Nuggets 2021 Spring Recruitment Campaign”, click to see the details of the campaign