LintCode 1301. The Game of Life JavaScript Algorithm

description

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

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

1. If the number of living cells in the eight locations around the living cells is less than two, the living cells in that location die;
2. If there are two or three living cells in the eight locations around the living cells, the living cells in that location are still alive ;
3, if there are eight positions around living cells over three living cells, the location of a live cell death;
4, if the dead cells around exactly three live cells, the position of the dead cells resurrection;

Based on the current state, write a function to calculate the next (after an update) state of the cell on the panel. 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 cell occur simultaneously.

Description

Are both a and b 32-bit integers?

  • Yes

Can I use bitwise operators?

  • of course can

Sample

样例 :

输入: 
[
  [0,1,0],
  [0,0,1],
  [1,1,1],
  [0,0,0]
]
输出: 
[
  [0,0,0],
  [1,0,1],
  [0,1,1],
  [0,1,0]
]

challenge

  • Can you use the in-situ algorithm to solve this problem? Please note that all grids on the panel need to be updated at the same time: you cannot update some grids first, and then use their updated values ​​to update other grids.
  • In this question, we use a two-dimensional array to represent the panel. In principle, the panel is infinite, but when living cells encroach on the boundary of the panel can cause problems. How will you solve these problems?

Parsing

gameOfLife = board => {
    
    
    if (board === null || board.length === 0) return;
    m = board.length, n = board[0].length;
    for (i = 0; i < m; i++) {
    
    
        for (j = 0; j < n; j++) {
    
    
            lives = h(board, m, n, i, j);
            if (board[i][j] === 1 && lives >= 2 && lives <= 3)  board[i][j] = 3; 
            if (board[i][j] === 0 && lives == 3)  board[i][j] = 2; 
        }
    }
    for (i = 0; i < m; i++) {
    
    
        for (j = 0; j < n; j++) {
    
    
            board[i][j] >>= 1;  
        }
    }
}
    
function h(board, m, n, i, j) {
    
    
    lives = 0;
    for (x = Math.max(i - 1, 0); x <= Math.min(i + 1, m - 1); x++) {
    
    
        for (y = Math.max(j - 1, 0); y <= Math.min(j + 1, n - 1); y++) {
    
    
            lives += board[x][y] & 1;
        }
    }
    lives -= board[i][j] & 1;
    return lives;
}

operation result

Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/SmallTeddy/article/details/108635537