Punch card question 4

 #左耳音风 ARST check-in activity restart#

 Table of contents

1. Topic 

2. Problem-solving code

 3. Problem-solving ideas


 Interpretation of ARTS - Complete one ARTS per week:
● Algorithm: Do at least one LeetCode algorithm problem per week
● Review: Read and comment on at least one English technical article
● Tips: Learn at least one technical skill
● Share: Share one Technical articles with opinions and thoughts

It is hoped that through this event, a wave of people who love technology can be gathered, and the spirit of curiosity, exploration, practice, and sharing can be continued.
 


1. Topic 

According to Baidu Encyclopedia, the Game of Life, referred to as Life for short, is a cellular automaton invented by British mathematician John Horton Conway in 1970.

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

If the number of living cells in the eight positions around the living cells is less than two, the living cells in this position are dead;
if there are two or three living cells in the eight positions around the living cells, the living cells in this position are still alive;
if the living cells If there are more than three living cells in the surrounding eight positions, the living cells in this position die;
if there are exactly three living cells around the dead cells, the dead cells in this position are resurrected;
the next state is obtained by applying the above rules to the current state at the same time Each cell is formed in which the birth and death of cells occur simultaneously. Gives you the current state of the mxn grid panel board, returns the next state.

Example 1:


Input: board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]]
Output: [[0,0,0],[1 ,0,1],[0,1,1],[0,1,0]]

Example 2:


Input: board = [[1,1],[1,0]]
Output: [[1,1],[1,1]]

2. Problem-solving code

def gameOfLife(board):
    def getNextBoard(board):
        m, n = len(board), len(board[0])
        nextBoard = [[0] * n for _ in range(m)]
        for i in range(m):
            for j in range(n):
                liveNeighbors = sum([board[x][y] for x in range(max(i-1, 0), min(i+2, m)) for y in range(max(j-1, 0), min(j+2, n))]) - board[i][j]
                if board[i][j] == 1:
                    if liveNeighbors < 2 or liveNeighbors > 3:
                        nextBoard[i][j] = 0
                    else:
                        nextBoard[i][j] = 1
                elif liveNeighbors == 3:
                    nextBoard[i][j] = 1
        return nextBoard

    while True:
        newBoard = getNextBoard(board)
        if newBoard == board:
            break
        board = newBoard
    return board

 3. Problem-solving ideas

This code implements a function called `gameOfLife` for solving the Game of Life problem.

The Game of Life is a discrete model based on cellular automata, which is represented by a two-dimensional matrix, each element represents a cell, and the initial state is 0 or 1. The state of each cell is updated according to the state of its eight neighbors, the specific rules are as follows:

- A living cell dies if there are less than two living cells around it;
- A living cell survives if there are two or three living cells around it;
- A living cell survives if there are more than three living cells around it live cell, the cell dies;
- if there are exactly three live cells around a dead cell, the cell is revived.

The `gameOfLife` function accepts a two-dimensional list `board` as a parameter, representing the current state of the game of life. An auxiliary function `getNextBoard` is defined inside the function, which is used to calculate the next state of the game of life. This helper function first creates a new two-dimensional list `nextBoard`, which is used to store the next state. Then traverse each element of the current state (that is, each cell), calculate the state of the cell in the next state according to the above rules, and save it in `nextBoard`. Finally return `nextBoard` as result.

In the `gameOfLife` function, a while loop is used to keep calculating the next state until the next state is the same as the current state. This is because the Game of Life is an iterative process, and the state changes after each iteration, so it needs to go on forever to get the final result.

Guess you like

Origin blog.csdn.net/m0_49914128/article/details/132008122