-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2023_06_09_group3.py
69 lines (58 loc) · 1.47 KB
/
2023_06_09_group3.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
area1 = [
["*", ".", ".", "."],
[".", ".", ".", "."],
[".", "*", ".", "."],
[".", ".", ".", "."]
]
result_area1 = [
["*", "1", "0", "0"],
["2", "2", "1", "0"],
["1", "*", "1", "0"],
["1", "1", "1", "0"]
]
area2 = [
["*", "*", ".", ".", "."],
[".", ".", ".", ".", "."],
[".", "*", ".", ".", "."]
]
result_area2 = [
['*', '*', '1', '0', '0'],
['3', '3', '2', '0', '0'],
['1', '*', '1', '0', '0']
]
directions = [
(-1, -1),
(-1, 0),
(-1, 1),
(0, -1),
(0, 1),
(1, -1),
(1, 0),
(1, 1),
]
def solve_minesweeper(area: list[list[str]]):
num_rows = len(area)
num_columns = len(area[0])
for row in range(num_rows):
for column in range(num_columns):
value = area[row][column]
if value != ".":
continue
bomb_count = 0
for dr, dc in directions:
new_row, new_col = row + dr, column + dc
if 0 <= new_row < num_rows and 0 <= new_col < num_columns:
if area[new_row][new_col] == "*":
bomb_count += 1
area[row][column] = str(bomb_count)
import pprint
pprint.pprint(area)
return area
if __name__ == '__main__':
print("Solving area 1:")
assert solve_minesweeper(area1) == result_area1
print()
print("Solving area 2:")
assert solve_minesweeper(area2) == result_area2
print()
print("Success! No errors :D")