-
Notifications
You must be signed in to change notification settings - Fork 17
/
minesweeper.py
86 lines (71 loc) · 2.6 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from functools import lru_cache
class Solution:
def updateBoard(self, board, click):
row, col = click[0], click[1]
if board[row][col] == "M":
board[row][col] = "X"
return board
def cells_around(row, col):
for cur_row, cur_col in [
(row - 1, col - 1), (row - 1, col), (row - 1, col + 1),
(row, col - 1), (row, col + 1),
(row + 1, col - 1), (row + 1, col), (row + 1, col + 1),
]:
if 0 <= cur_row < len(board) and 0 <= cur_col < len(board[0]):
yield cur_row, cur_col
@lru_cache(None)
def backtrack(row, col):
mines = sum(
[
1 for cur_row, cur_col in cells_around(row, col) \
if board[cur_row][cur_col] == "M"
]
)
if mines > 0:
board[row][col] = str(mines)
elif board[row][col] != "B": # Initial cell is not in lru_cache till the end
board[row][col] = "B"
for cur_row, cur_col in cells_around(row, col):
backtrack(cur_row, cur_col)
backtrack(row, col)
return board
class TestSolution:
def setup(self):
self.sol = Solution()
def test_two(self):
assert self.sol.updateBoard([['M']], [0, 0]) == [['X']]
assert self.sol.updateBoard([['E']], [0, 0]) == [['B']]
def test_three(self):
assert self.sol.updateBoard([['M', 'E']], [0, 0]) == [['X', 'E']]
assert self.sol.updateBoard([['E', 'E']], [0, 0]) == [['B', 'B']]
assert self.sol.updateBoard([['E', 'M']], [0, 0]) == [['1', 'M']]
def test_one(self):
assert self.sol.updateBoard(
[
['E', 'E', 'E', 'E', 'E'],
['E', 'E', 'M', 'E', 'E'],
['E', 'E', 'E', 'E', 'E'],
['E', 'E', 'E', 'E', 'E'],
],
[3, 0],
) == [
['B', '1', 'E', '1', 'B'],
['B', '1', 'M', '1', 'B'],
['B', '1', '1', '1', 'B'],
['B', 'B', 'B', 'B', 'B'],
]
def test_four(self):
assert self.sol.updateBoard(
[
['B', '1', 'E', '1', 'B'],
['B', '1', 'M', '1', 'B'],
['B', '1', '1', '1', 'B'],
['B', 'B', 'B', 'B', 'B']
],
[1, 2],
) == [
['B', '1', 'E', '1', 'B'],
['B', '1', 'X', '1', 'B'],
['B', '1', '1', '1', 'B'],
['B', 'B', 'B', 'B', 'B']
]