-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudoku solver.py
110 lines (92 loc) · 2.9 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
def show_sudoku(sudoku,height,width):
for line in sudoku:
print('|',end='')
for char in line:
print(char,'|',end='')
print('')
pass
def solve_sudoku(sudoku,width,height,S_width,S_height):
border = max(height,width)
possibilities = [i+1 for i in range(border)]
def check_tile(x,y):
element = sudoku[y][x]
rectangle = []
W2 = x//S_width*S_width
H2 = y//S_height*S_height
for i in range(H2,H2+S_height):
rectangle+= sudoku[i][W2:W2+S_width]
L = [0,0,0]
#testing elements in rectangle
for i in rectangle:
if element == i:
if L[0] >=1:
#print("Rectangle")
return False
L[0]+=1
#testing elements in row
for i in sudoku[y]:
if element == i:
if L[1] >=1:
#print("Row")
return False
L[1]+=1
#testing elements in column
for i in sudoku:
if element == i[x]:
if L[2] >=1:
#print("Column")
return False
L[2]+=1
return True
def check_filed(sudoku):
for line in sudoku:
for el in line:
if el==0:
return False
return True
def fill_tile(x,y):
#print("{",y,'}',"{",x,'}',)
if sudoku[y][x] != 0:
x+=1
if x >= width:
x=0
y+=1
if y>=height:
return 0
fill_tile(x,y)
else:
for i in possibilities:
sudoku[y][x] = i
# show_sudoku(sudoku,width,height)
# print('')
if check_tile(x,y):
if check_filed(sudoku):
print("Solution found!")
show_sudoku(sudoku,height,width)
xSaved = x
ySaved = y
x+=1
if x >= width:
x=0
y+=1
if y>=height:
sudoku[-1][-1] = 0
return 0
fill_tile(x,y)
x = xSaved
y = ySaved
sudoku[y][x] = 0
fill_tile(0,0)
SUDOKU = [
[0,1,0,5,0,0,3,0,0],
[0,0,2,8,0,0,0,0,0],
[0,0,3,0,0,0,1,9,0],
[0,2,0,0,0,9,0,1,0],
[6,4,0,0,0,0,0,5,0],
[5,0,0,0,0,1,0,2,0],
[0,0,0,0,7,0,0,0,6],
[0,0,0,0,6,2,0,0,7],
[0,9,0,0,0,0,0,0,0],
]
solve_sudoku(SUDOKU,9,9,3,3)
#show_sudoku(SUDOKU1,4,4)