Leetcode:game of Life-Conway games

Source: Internet
Author: User

1. Title

Game of Life (Conway Lifetime game)

2. Address of the topic

Https://leetcode.com/problems/game-of-life

3. Topic content

English:

According to the Wikipedia's article: "The Gameof Life, also known simply as Life , is a cellular automa ton 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.

Chinese:

According to Wikipedia article Conway's Game of Life (Conway Lifetime game), Conway Life Game is a cellular automaton invented by British mathematician John Hoton Kangwei in 1970.

A m*n cell matrix is given, with each cell having an initial state: Survival (1) or death (0). Each cell changes to 8 cells around it, and the rules are as follows:

    1. When the current cell is alive, the cell becomes dead when there are less than 2 surviving cells in the surrounding state. (Simulation of the number of life is scarce)

    2. When the current cell is alive, the cell remains intact when there are 2 or 3 surviving cells around it.

    3. When the current cell is alive, the cell becomes dead when there are more than 3 surviving cells in the surrounding state. (excessive number of simulated life)

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

Write a function that calculates the next state of the cell matrix based on the current state of the Matrix.

4. Methods of Solving problems

Without using the definition of a new matrix, in order to solve this problem, we need to define a set of intermediate states, the intermediate state requirements can see a cell change before and after two aspects of life and death situation. The Java code is as follows:

/** * @ function Description: leetcode 289 - game of life * @ developer:tsybius2014  * @ Development Time: October 8, 2015  */public class solution {    //death Unit      final int dead = 0;    //Survival Unit      final int alive = 1;    //change: death → Death      final int dead_to_dead = 0;    //change: Survival → Survival      final int alive_to_alive = 1;    //change: Survival → Death      final int alive_to_dead = 2;    //change: death → Survival      final int dead_to_alive = 3;        /**      *  determine if a point is dead before the change in this round      *  @param  obj      *  @return &Nbsp;    */    private boolean isaliveold (Int obj)  {        if  (obj == alive_to_alive | |  obj == alive_to_dead)  {             return true;        }         else {            return  false;        }    }    /**      *  determine if a point is dead after the change in this round      *  @param  obj      *  @return      */    private  boolean isalivenew (int obj)  {        if  (obj &NBSP;%&NBSP;2&NBSP;==&NBSP;1)  {            return true;         } else {             return false;        }    }         /**     *  Life Games       *  @param  board     */    public void  Gameoflife (int[][] board)  {        //input Legality check          if  (board == null)  {             return;        }         int height = board.length;         if  (Height == 0)  {            return;         }        int  width = board[0].length;        if  (width ==  0)  {            return;         }        //examines all points and changes their life state          int counter = 0;         for  (int i = 0; i < height; i++)  {             for  (int j = 0; j <  width; j++)  {                                 //statistics surrounding life conditions                  counter = 0;                 if  (i >  0 && j > 0 && isaliveold (board[i - 1][j  -&NBSP;1])  {                     counter++;                 }                 if  (I > 0 && isaliveold (BOARD[I&NBSP;-&NBSP;1][J))  {                     counter++;                }                 if  (i >  0 && j < width - 1 && isaliveold (board[i - &NBSP;1][J&NBSP;+&NBSP;1])  {                     counter++;                 }                 if  (J > 0 && isaliveold (board[i][j &NBSP;-&NBSP;1])  {                     counter++;                 }                 if  (J  < width - 1 && isaliveold (board[i][j + 1])  {                      counter++;                 }                if   (i < height - 1 && j > 0 &&  Isaliveold (board[i + 1][j - 1])  {                     counter++;                 }                 if  (I < height - 1 && isaliveold (board[i &NBSP;+&NBSP;1][J])  {                     counter++;                 }                 if  (i < height - 1 && j <  width - 1 && isaliveold (board[i + 1][j + 1])  {                      counter++;                 }                                 //determines the current point change based on life conditions around the specified point                  if  (Isaliveold (Board[i][j]) )  {                     if  (counter < 2)  {                         //1. Survival units around the survival unit of less than 2, the unit died                          board[i][j] = ALIVE_TO_DEAD;                     }  else if  (counter == 2 | | &NBSP;COUNTER&NBSP;==&NBSP;3)  {                         //2. There are 2-3 surviving units around the surviving unit, and the unit continues to survive                          board[i][j] = ALIVE_TO_ALIVE;                      } else {               &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//3. Survival units around the surviving Unit 3 extra, the unit died                           board[i][j] = ALIVE_TO_DEAD;                     }                 } else {                     if  (counter == 3)  {                         &NBSP;//4. The surviving unit around the death unit is exactly 3, and the unit becomes a viable State                          board[i][j] = DEAD_TO_ALIVE;                                           } else {                         board[i][j] = dead_to_ Dead;                    }                 }             }        }         //the life and death of each point according to the transformed survival status          for  (int i = 0; i < height; i++)  {             for  (int j = 0; j <  width; j++)  {                 if  (Isalivenew (board[i][j))  {                     board[i][j] = ALIVE;                 } else {                     board[i][j] =  dead;                }             }         }    }}

END

Leetcode:game of Life-Conway games

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.