-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminesweeper.py
51 lines (40 loc) · 1.97 KB
/
minesweeper.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class Solution(object):
def updateBoard(self, board, click):
"""
:type board: List[List[str]]
:type click: List[int]
:rtype: List[List[str]]
"""
adjs = [[-1, 0], [-1, 1], [0, 1], [1, 1], [1, 0], [1, -1], [0, -1], [-1, -1]]
visited = [[0 for j in range(len(board[0]))] for i in range(len(board))]
def bfs(i, j, board):
queue = [(i, j)]
while queue:
next = queue.pop(0)
mc = 0
if not visited[next[0]][next[1]]:
visited[next[0]][next[1]] = 1
if board[next[0]][next[1]] == 'E':
board[next[0]][next[1]] = 'B'
if board[next[0]][next[1]] == 'M':
continue
for adj in adjs:
adji = adj[0] + next[0]
adjj = adj[1] + next[1]
if adji >= 0 and adjj >= 0 and adji <= len(board)-1 and adjj <= (len(board[0]))-1:
if board[adji][adjj] == 'M':
mc += 1
if mc == 0:
for adj in adjs:
adji = adj[0] + next[0]
adjj = adj[1] + next[1]
if adji >= 0 and adjj >= 0 and adji <= len(board)-1 and adjj <= (len(board[0]))-1:
if not visited[adji][adjj]:
queue.append((adji, adjj))
else:
board[next[0]][next[1]] = str(mc)
if board[click[0]][click[1]] == 'M':
board[click[0]][click[1]] = 'X'
return board
bfs(click[0], click[1], board)
return board