[Leetcode] [JavaScript] Game of Life

Source: Internet
Author: User

Game of Life

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):

    1. Any live cell with fewer than-live neighbors dies, as if caused by under-population.
    2. Any live cell with a or three live neighbors lives on to the next generation.
    3. Any live cell with more than three live neighbors dies, as if by over-population.
    4. 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:

    1. 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.
    2. 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?

Game of life, requires in-place, that is, only in the board array, do not open extra array space.

Tidy up the rules:

1. A neighbor less than 2 or more than 3 will die;

2. The neighbor equals 2, the state is unchanged, what is the original or what;

3. The neighbor equals 3, survives, regardless of the original state.

In-place mainly need to solve a problem, when dealing with a cell, can not be judged according to the state after the change, according to the original state.

The first round of processing is nothing more than 4 cases: 0, 0, 1, 1, 0, 1, 1, 0.

0--0, 1--1 No problem, we put 0--1 into 2, 1--0 to 3.

1 and 3 are considered to be alive when judging neighbors.

The second cycle turns all 2 into 1 and 3 to 0.

1 /**2 * @param {number[][]} board3 * @return {void} does not return anything, modify board In-place instead.4  */5 varGameoflife =function(board) {6     varI, J;7      for(i = 0; i < board.length; i++){8          for(j = 0; J < Board[i].length; J + +){9             varCurr =Board[i][j];Ten             varNeighbors =Countneighbors (i, j); One             if(Neighbors < 2 | | Neighbors > 3){ A                 if(Curr = = 1){ -BOARD[I][J] = 3; -                 } the}Else if(Neighbors = = 3){ -                 if(Curr = = 0){ -BOARD[I][J] = 2; -                 } +             } -         } +     } A      for(i = 0; i < board.length; i++){ at          for(j = 0; J < Board[i].length; J + +){ -             if(Board[i][j] = = = 2){ -BOARD[I][J] = 1; -}Else if(Board[i][j] = = = 3){ -BOARD[I][J] = 0; -             } in         } -     } to  +  -     functionCountneighbors (i, j) { the         varCount = 0; *Count + = isAlive (i, j-1); Count + = IsAlive (i, j + 1); $Count + = isAlive (i-1, j-1); Count + = IsAlive (I-1, J); Count + = IsAlive (i-1, J + 1);Panax NotoginsengCount + = isAlive (i + 1, j-1); Count + = isAlive (i + 1, j); Count + = isAlive (i + 1, j + 1); -         returncount; the     } +     functionisAlive (i, j) { A         if(!board[i] | |!Board[i][j]) { the             return0; +         } -         varCell =Board[i][j]; $         if(cell = = = 1 | | cell = = = 3){ $             return1; -         } -         return0; the     } -};

[Leetcode] [JavaScript] Game of Life

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.