-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
154 lines (109 loc) · 3.5 KB
/
main.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import copy
import os
import random
import time
def is_valid(board, row, col, num):
# check is num legal in row
if num in board[row]:
return False
# check is num legal in col
for i in range(9):
if num == board[i][col]:
return False
# check is num legal in 3*3 matrix
start_row, start_col = 3 * (row // 3), 3 * (col // 3)
for r in range(start_row, start_row + 3):
for c in range(start_col, start_col + 3):
if num == board[r][c]:
return False
return True
def fill_board(board, row=0, col=0):
if row == 9:
return True
if col < 8:
next_row, next_col = row, col + 1
else:
next_row, next_col = row + 1, 0
if board[row][col] != 0:
return fill_board(board, next_row, next_col)
for num in random.sample(range(1, 10), 9):
if is_valid(board, row, col, num):
board[row][col] = num
if fill_board(board, next_row, next_col):
return True
board[row][col] = 0
return False
def check_remove(board, row=0, col=0, counter=None):
if counter is None:
counter = [0]
if row == 9:
counter[0] += 1
return
# end recursion if not only one solution
if counter[0] > 1:
return True
if col < 8:
next_row, next_col = row, col + 1
else:
next_row, next_col = row + 1, 0
for num in range(1, 10):
if is_valid(board, row, col, num):
board[row][col] = num
check_remove(board, next_row, next_col, counter)
board[row][col] = 0
return False
def remove_num(board, count):
board_copy = copy.deepcopy(board)
current_count = 0
while current_count < count:
row = random.randint(0, 8)
col = random.randint(0, 8)
while board_copy[row][col] == 0:
row = random.randint(0, 8)
col = random.randint(0, 8)
counter = [0]
remove_num = board_copy[row][col]
board_copy[row][col] = 0
check_remove(board_copy, counter=counter)
if counter[0] > 1:
board_copy[row][col] = remove_num
else:
current_count += 1
return board_copy
def solve(board, row=0, col=0, start_line=0):
if row == 9:
return True
if col < 8:
next_row, next_col = row, col + 1
else:
next_row, next_col = row + 1, 0
if board[row][col] != 0:
return solve(board, next_row, next_col, start_line)
for num in range(1, 10):
if is_valid(board, row, col, num):
board[row][col] = num
print_board(board, start_line)
if solve(board, next_row, next_col, start_line):
return True
else:
board[row][col] = 0
return False
def print_board(board, start_line):
# local the start line
print(f"\033[{start_line}H", end="")
# clear content form the start line to end
print("\033[J", end="")
for row in board:
print(" ".join(str(num) if num != 0 else '.' for num in row))
time.sleep(0.01)
if __name__ == '__main__':
os.system('cls') # clear content in cmd line
# define a 9*9 matrix
board = [[0 for _ in range(9)] for _ in range(9)]
fill_board(board)
new_board = remove_num(board, 35)
for row in new_board:
print(" ".join(str(num) if num != 0 else '.' for num in row))
print("\n-------------------------------\n")
start_line = 13
solve(new_board, start_line=start_line)