-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path529_minesweeper.py
More file actions
37 lines (31 loc) · 1.07 KB
/
529_minesweeper.py
File metadata and controls
37 lines (31 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from typing import List
class Solution:
def updateBoard(self, board: List[List[str]], click: List[int]) -> List[List[str]]:
row = len(board)
if row == 0:
return board
column = len(board[0])
cx, cy = click[0], click[1]
if board[cx][cy] == 'M':
board[cx][cy] = 'X'
return board
elif board[cx][cy] != 'E':
return board
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]
def dfs(r, c):
count = 0
for i in range(8):
x, y = r + dx[i], c + dy[i]
if 0 <= x < row and 0 <= y < column and board[x][y] == 'M':
count += 1
if count > 0:
board[r][c] = chr(ord('0') + count)
else:
board[r][c] = 'B'
for i in range(8):
x, y = r + dx[i], c + dy[i]
if 0 <= x < row and 0 <= y < column and board[x][y] == 'E':
dfs(x, y)
dfs(cx, cy)
return board