[LeetCode-Golang] 289. Game of Life

topic

According to Baidu Encyclopedia, Game of Life, referred to as life, is a British mathematician John Horton Conway cellular automata 1970 invention.

Given a, m × n grid panels, each grid can be seen as a cell. Each cell having an initial state: 1 is the viable cells (live), that is, 0, or dead cells (dead). Its eight neighboring cells in each cell position (horizontal, vertical, diagonal) have followed the law of survival of the following four:


    If the number of viable cells in eight locations around fewer than two live cells, the location of a live cell death;
    If there are eight positions around the living cells of two or three living cells, the location of living cells were still alive;
    if there are eight positions around the living cells of more than three living cells, the location of a live cell death;
    around if just dead cells there are three live cells, dead cells revive the position;


the current state, a write function to calculate the next state (after update) on all the cells of the panel. The next state is formed by the above rules simultaneously applied to each cell in the current state, wherein the birth and death of cells occurs simultaneously.

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/game-of-life
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

Problem-solving ideas

Directly through the array, according to the modified state conditions.

Two things to note only

1. Offset 8 positions around the two-dimensional array can be listed, and then acquires the position around the loop, but also to facilitate the array bounds checking.

2. When traversing cell array, if a location directly change the state, will lead to a position behind misjudged the number of living cells. Thus the array can be replicated; original array may be used there have been no state values to be modified, and then again traversed the array is replaced with 0 or 1.

Code

gameOfLife FUNC (Board [] [] int) { 
    IF == nil || len Board (Board) == 0 { 
        return 
    } 
    // offset directions. 8 
    offset: = [] [] int {{-1, -1}, {- 1,0}, {- 1,1}, {0, -1}, {0,1}, {1, -1}, {1,0}, {1,1}} 
    // number of rows and columns of the array 
    row, COL: = len (Board), len (Board [0]) 
    / * 
        in accordance with a state of the current cell is the problem that sound while updating the state, so the cells can not be modified directly in the original array status is 0 or 1. 
        You can copy arrays (chips), traversing the original array, and modify the status of the copy in the array, it returns an array copy, but to open up extra space. 
        Int type array is noted that, in the first pass changes the original array with the other figures represent a state, then the second pass is reduced to 0,1. 
        Where by the position 0-> 1 to 2, by the position 1-> 0 to -1. 
    * / 
    // iterate 
    for I: = 0; I <Row; I ++ { 
        for J: = 0; J <COL; J ++ { 
            // record the current location adjacent the number of viable cells 
            liveCnt: = 0
            // iterate adjacent cells3 {
                    board[i][j] = -1
            for K: = 0; K <. 8; K ++ { 
                currX, Curry: = I + offset [K] [0], J + offset [K] [. 1] 
                // check array bounds 
                if currX> = 0 && currX < row Curry &&> = 0 && Curry <COL { 
                    // check the cell state (position and a value of -1 are updated before the live cells) 
                    IF Board [currX] [Curry] == 1 || Board [currX] [ Curry] == -1 { 
                        liveCnt ++ 
                    } 
                } 
            } 
            // determine the current state of the cell, and modified in accordance with the law of survival 
            if board [i] [j] == 1 { 
                number of viable cells surrounding a living cell // <2 or> 3, the living cell death 
                if liveCnt <2 || liveCnt>
            } else if liveCnt == 3{
                } 
                // Number of dead cells around == 3 live cells, dead cells revive the 
                Board [I] [J] = 2 
            } 
        } 
    } 
    // iterate second, -1 is becomes into 0,2. 1 
    for I : = 0; I <Row; I ++ { 
        for J: = 0; J <COL; J ++ { 
            IF Board [I] [J] == -1 { 
                Board [I] [J] = 0 
            } the else IF Board [I ] [J] == {2 
                Board [I] [J] =. 1 
            } 
        } 
    } 
}

 

Complexity Analysis

Time complexity: O (mn)
space complexity: O (1)

Results of the

When execution: 0 ms, defeated 100.00% of users in all Go submission
Memory consumption: 2 MB, defeated 100.00% of users in all Go submission

Guess you like

Origin www.cnblogs.com/nini-skyrim/p/12617163.html