Leetcode notes: Game of Life

Source: Internet
Author: User

Leetcode notes: Game of Life

I. Description

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.

Ii. Question Analysis

The question is very long and the general idea is as follows:

Specify0,1Matrix, each element indicates the survival of a cell, 1 survival, 0 death, the survival of each cell is determined by the top, bottom, left, right, top left, bottom left, top right, and bottom right of each cell in the next update. The survival rules are as follows:

When no more than two surviving cells exist, the cells become dead. (The number of simulated lives is scarce)

When the current cell is in the active state and there are 2 or 3 surviving cells around it, the cell remains unchanged.

When the current cell is in the active state and more than three surviving cells exist around it, the cell becomes dead. (Too many simulated lives)

When the current cell is in the dead state, when there are exactly three surviving cells around it, the cell becomes alive. (Simulated reproduction)

Write a function to calculate the next state of the cell matrix based on the current state of the matrix.

The question requires an in-place operation. Considering that each element is affected by eight nearby elements, an intermediate state must be used to record element changes. This intermediate state should be able to reflect elements.Before changeAndAfter change.

Fortunately, the question provides four changing states, and provides a more intuitive way of thinking:

Status 0: dead cells-> dead cells
Status 1: living cell-> living cell
Status 2: living cell-> dead cell
Status 3: dead cells> Living Cells

Determine each element and obtain the State based on the eight nearby elements and their own States.0~4But the next element can still be based on the status of the previous element.0~4To determine the status before the previous element changes.

Mark the status of all elements.0~4Then, after traversing the matrix again, if the State of all elements is equal to 2, the States 0 and 2 become dead cells, and the states 1 and 3 are living cells to achieve their purpose.

Iii. Sample Code

class Solution{public:  void gameOfLife(vector
  
   >& board)  {    int rows=board.size();    if(rows==0)      return ;    int colums=board[0].size();    if(colums==0)      return ;    for(int i=0; i
   
    >& board,int rows,int colums,int x,int y)  {    int sum=0;    for(int i=x-1; i
    
     =0&&i
     
      =0&&j
      
     
    
   
  

Iv. Summary

This is an interesting question at the in-place requirement.

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.