leetcode brush 2019-5-10 --- title series Game of Life

289 Game of Life

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 has an initial state live (1) that is live cells or dead (0) is the dead cells. Per cell to its eight neighboring positions (horizontal, vertical, diagonal) are the following four cell survival Law:

  • If the number of viable cells of eight positions 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 living cells over three living cells, the location of a live cell death;
  • If you happen to have three dead cells around the living cell, the position of the dead cells resurrection;
  • According to the current state, a write function to calculate the next state (after the last update) cells on 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.

Example:

输入: 
[
  [0,1,0],
  [0,0,1],
  [1,1,1],
  [0,0,0]
]
输出: 
[
  [0,0,0],
  [1,0,1],
  [0,1,1],
  [0,1,0]
]

Problem-solving ideas:

The meaning of the questions are based on the step by step can be solved the problem, but can not be modified while traversing the side, it can only be traversed again, good record which cells need to change the status. And then update the status at the time of the second pass traversal. Increasing condition only a few cells, as is now represents the next state is live and live; Representative now live. 3 but in a dead state; Representative now dead. 4 but in a live state, the following code:

class Solution:
    def gameOfLife(self, board: List[List[int]]) -> None:
        """
        Do not return anything, modify board in-place instead.
        """
        #// 当前是活的,下一刻活的:2
        #// 当前是活的,下一刻死的:3
        #// 当前是死的,下一刻活的:4
        m = len(board) # 行
        n = len(board[0]) # 列
        for i in range(m):
            for j in range(n):
                # 判断周围细胞情况
                livecount = 0
                for x in range(max(0,i-1),min(m,i+2)):
                    for y in range(max(0,j-1),min(n,j+2)):
                        if (board[x][y] == 1 or board[x][y] == 2 or board[x][y] == 3)and not (x == i and y == j):
                            livecount += 1

                # 根据周围的细胞数量改变状态
                if (board[i][j] == 1 or board[i][j] == 2 or board[i][j] == 3) : # 细胞活的
                    if livecount < 2 or livecount > 3:
                        board[i][j] = 3
                else:
                    if livecount == 3:
                        board[i][j] = 4
                    else:
                        board[i][j] = 0
        for i in range(m):
            for j in range(n):
                if board[i][j] == 3:
                    board[i][j] = 0
                if board[i][j] == 4:
                    board[i][j] = 1

Guess you like

Origin blog.csdn.net/weixin_43615373/article/details/90084818