-
Notifications
You must be signed in to change notification settings - Fork 310
/
Copy pathminesweeper.py
46 lines (41 loc) · 1.43 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
# Source : https://leetcode.com/problems/minesweeper/?tab=Description
# Author : Han Zichi
# Date : 2017-03-07
class Solution(object):
def updateBoard(self, board, click):
"""
:type board: List[List[str]]
:type click: List[int]
:rtype: List[List[str]]
"""
n, m = len(board), len(board[0])
dir = [[1, 0], [-1, 0], [0, 1], [0, -1], [-1, -1], [-1, 1], [1, -1], [1, 1]]
def isNotInMap(x, y):
return x < 0 or x >= n or y < 0 or y >= m
def getMineNum(x, y):
num = 0
for i in range(8):
_x, _y = x + dir[i][0], y + dir[i][1]
if isNotInMap(_x, _y):
continue
if board[_x][_y] == 'M':
num += 1
if num == 0:
return 'B'
else:
return str(num)
def dfs(x, y):
if board[x][y] == 'M':
board[x][y] = 'X'
else:
board[x][y] = getMineNum(x, y)
if board[x][y] == 'B':
for i in range(8):
_x, _y = x + dir[i][0], y + dir[i][1]
if isNotInMap(_x, _y):
continue
if board[_x][_y] != 'E': # not an empty cell
continue
dfs(_x, _y)
dfs(click[0], click[1])
return board