-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSudoku_Solver.py
179 lines (153 loc) · 6.18 KB
/
Sudoku_Solver.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
'''
Sudoku_Solver - A basic program to input a sudoku and find all possible solutions
Copyright (C) 20223 Balakrishna Prabhu B. N. <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
-----------------------------------------------------------------------------------------------------------------------
This program will take in a 9*9 matrix of sudoku as an input and then return the solved sudoku as output.
For that we will use functions.
1) To check weather a number is possible at a position.
- For that we have to check the row the column and the 3*3 square it belongs to.
-in order to check which 3*3 square it belong to we will use another function.
2) To find out which 3*3 square the given element belong to.
- This will take the indices of the element as the input and then return a number from 1 - 9.
This did not work properly as brute force cannot solve all logic.
Hence using backtracking and recursion to solve the problem.
-----------------------------------------------------------------------------------------------------------------------
'''
def show_sudoku():
# print('')
for y in range(9):
for x in range(9):
print(matrix[y][x], end=' ')
print('')
def get_sudoku():
print('Please enter the sudoku.(not Comma separated eg:123456789 )')
sudoku = [[] for j in range(9)]
for i in range(9):
sudoku[i] = list(map(int, list(input('Row ' + str(i + 1) + ' : '))))
return sudoku
def possible(number, y, x):
# returns true if possible.
# Checking Row.
for row in range(9):
if matrix[row][x] == number:
return False
# Checking Column.
for column in range(9):
if matrix[y][column] == number:
return False
# Checking Small Square.
x_0 = x // 3 * 3
y_0 = y // 3 * 3
for i in range(0, 3):
for j in range(0, 3):
if matrix[y_0 + i][x_0 + j] == number:
return False
return True
def solve():
global matrix
for y in range(0, 9):
for x in range(0, 9):
if matrix[y][x] == 0:
for num in range(1, 10):
if possible(num, y, x):
matrix[y][x] = num
solve()
matrix[y][x] = 0
return
global num_sol
num_sol += 1
print('Solution Number :', num_sol)
show_sudoku()
# input('More?')
print('')
# Below are some matrix made for testing purposes so that we need not type in each time.
mat = [[0, 0, 0, 2, 6, 0, 7, 0, 1],
[6, 8, 0, 0, 7, 0, 0, 9, 0],
[1, 9, 0, 0, 0, 4, 5, 0, 0],
[8, 2, 0, 1, 0, 0, 0, 4, 0],
[0, 0, 4, 6, 0, 2, 9, 0, 0],
[0, 5, 0, 0, 0, 3, 0, 2, 8],
[0, 0, 9, 3, 0, 0, 0, 7, 4],
[0, 4, 0, 0, 5, 0, 0, 3, 6],
[7, 0, 3, 0, 0, 8, 0, 0, 0]] # mat1
mat2 = [[0, 0, 0, 0, 0, 0, 6, 8, 0],
[0, 0, 0, 0, 7, 3, 0, 0, 9],
[3, 0, 9, 0, 0, 0, 0, 4, 5],
[4, 9, 0, 0, 0, 0, 0, 0, 0],
[8, 0, 3, 0, 5, 0, 9, 0, 2],
[0, 0, 0, 0, 0, 0, 0, 3, 6],
[9, 6, 0, 0, 0, 0, 3, 0, 8],
[7, 0, 0, 6, 8, 0, 0, 0, 0],
[0, 2, 8, 0, 0, 0, 0, 0, 0]] # mat2
mat3 = [[5, 0, 0, 0, 7, 0, 0, 0, 0],
[6, 0, 0, 1, 9, 5, 0, 0, 0],
[0, 9, 8,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