题目描述
https://leetcode-cn.com/problems/minesweeper/submissions/
python
def updateBoard(self, board: List[List[str]], click: List[int]) -> List[List[str]]:
# M : boom
# E: empty place
if board[click[0]][click[1]] == "M":
board[click[0]][click[1]] = "X"
return board
row = len(board)
column = len(board[0])
Q = []
visited = []
Q.append(click)
visited.append(click)
directions = [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]] # 八个子节点
while Q:
tmp = Q.pop(0)
i = tmp[0]
j = tmp[1]
if board[i][j] == "E":
# 上下左右有
count = 0
tmp_list = []
for direction in directions:
x = i + direction[0]
y = j + direction[1]
if x not in range(row) or y not in range(column):
continue
if board[x][y] == "M":
count+=1
board[i][j] = str(count)
elif board[x][y] == "E" and [x, y] not in visited:
tmp_list.append([x,y])
if count == 0:
board[i][j] = "B"
for item in tmp_list:
Q.append(item)
visited.append(item)
return board