-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
147 lines (125 loc) · 3.61 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
'''
A brute force 9x9 sudoku solver using backtracking algorithm
This algorithm assumes there is only one possible solution to given board
if not it prints all solutions
if you want to print just one solution update solve function as:
def solve(b):
pos = find_next_blank_position(b)
if pos == None:
print_result(b)
return True
for val in range(1, 10):
if validation(pos, val, b):
fill_blank(pos, val, b)
if solve(b):
return True
else:
make_blank(pos, b)
return False
'''
# Board Definitions:
BOARD0 = [
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0]
]
BOARDX = [
[1, 2, 3, 4, 5, 6, 7, 8, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 2],
[0, 0, 0, 0, 0, 0, 0, 0, 3],
[0, 0, 0, 0, 0, 0, 0, 0, 4],
[0, 0, 0, 0, 0, 0, 0, 0, 5],
[0, 0, 0, 0, 0, 0, 0, 0, 6],
[0, 0, 0, 0, 0, 0, 0, 0, 7],
[0, 0, 0, 0, 0, 0, 0, 0, 9]
]
BOARD = [
[2, 7, 4, 0, 9, 1, 0, 0, 5],
[1, 0, 0, 5, 0, 0, 0, 9, 0],
[6, 0, 0, 0, 0, 3, 2, 8, 0],
[0, 0, 1, 9, 0, 0, 0, 0, 8],
[0, 0, 5, 1, 0, 0, 6, 0, 0],
[7, 0, 0, 0, 8, 0, 0, 0, 3],
[4, 0, 2, 0, 0, 0, 0, 0, 9],
[0, 0, 0, 0, 0, 0, 0, 7, 0],
[8, 0, 0, 3, 4, 9, 0, 0, 0]
]
BOARDZ = [
[0, 0, 5, 3, 0, 0, 0, 0, 0],
[8, 0, 0, 0, 0, 0, 0, 2, 0],
[0, 7, 0, 0, 1, 0, 5, 0, 0],
[4, 0, 0, 0, 0, 5, 3, 0, 0],
[0, 1, 0, 0, 7, 0, 0, 0, 6],
[0, 0, 3, 2, 0, 0, 0, 8, 0],
[0, 6, 0, 5, 0, 0, 0, 0, 9],
[0, 0, 4, 0, 0, 0, 0, 3, 0],
[0, 0, 0, 0, 0, 9, 7, 0, 0]
]
def print_result(b):
for r in range(9):
if r % 3 == 0 and r != 0:
print('- - - - - - - - - -')
for c in range(9):
if c % 3 == 0 and c!= 0:
print('|', end="")
print(str(b[r][c]) + ' ', end="")
print()
def find_next_blank_position(b):
for r in range(9):
for c in range(9):
if(b[r][c]==0):
return (r,c)
return None # no empty square
def validation(pos, val, b):
# validation of the rows
for item in range(9):
if b[pos[0]][item] == val and item != pos[1]:
return False
#validation of columns
for item in range(9):
if b[item][pos[1]] == val and item != pos[0]:
return False
#validation of boxes
# box array = [
# [0,0],
# [0,1],
# [0,2],
# [1,0],
# [1,1],
# [1,2],
# [2,0],
# [2,1],
# [2,2]
# ]
#box's row and column position in box array with integer division
box_r = (pos[0] // 3)
box_c = (pos[1] // 3)
for r in range(3*box_r, 3*box_r + 3):
for c in range(3*box_c, 3*box_c + 3):
if b[r][c] == val and pos != (r,c):
return False
# all criteria passed then the val is valid:
return True
def fill_blank(pos, val, b):
b[pos[0]][pos[1]] = val
def make_blank(pos, b):
b[pos[0]][pos[1]] = 0
def solve(b):
pos = find_next_blank_position(b)
if pos == None:
print_result(b)
return True
for val in range(1, 10):
if validation(pos, val, b):
fill_blank(pos, val, b)
solve(b)
make_blank(pos, b)
# this false triggers backtracking and makes above else condition valid to make the previos filled square blank
return False