-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.py
84 lines (75 loc) · 2.16 KB
/
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
def check(sudoku,i,j,n):
if n in sudoku[i]:
return False
elif n in column(sudoku,j):
return False
elif n in box(sudoku,i,j):
return False
else: return True
def column(sudoku,c):
return [sudoku[i][c] for i in range(9)]
def box(sudoku,i,j):
i1=i//3
j1=j//3
i2=i1*3
j2=j1*3
return [sudoku[x][y] for x in range(i2,i2+3) for y in range(j2,j2+3)]
def empty(sudoku):
for x in range(9):
for y in range(9):
if (sudoku[x][y]==0):
return [x,y]
return False
def solver(sudoku):
if empty(sudoku):
l=empty(sudoku)
x=l[0]
y=l[1]
for n in range(1,10):
if not(check(sudoku,x,y,n)): continue
else:
sudoku[x][y]=n
if not(solver(sudoku)):
sudoku[x][y]=0
continue
else: return True
else: return False
else:
print(sudoku)
return True
sudoku19=[[0, 0, 3, 0, 5, 0, 0, 0, 9],
[0, 0, 6, 0, 8, 0, 1, 0, 3],
[0, 8, 9, 1, 2, 0, 0, 5, 6],
[0, 3, 4, 0, 6, 7, 8, 9, 0],
[5, 0, 7, 8, 0, 0, 0, 3, 4],
[8, 9, 0, 2, 3, 0, 5, 6, 0],
[0, 0, 2, 0, 4, 5, 6, 0, 8],
[6, 7, 8, 0, 1, 2, 3, 4, 5],
[3, 4, 5, 6, 7, 8, 9, 0, 2]]
sudoku20=[[5,3,0,0,7,0,0,0,0],
[6,0,0,1,9,5,0,0,0],
[0,9,8,0,0,0,0,6,0],
[8,0,0,0,6,0,0,0,3],
[4,0,0,8,0,3,0,0,1],
[7,0,0,0,2,0,0,0,6],
[0,6,0,0,0,0,2,8,0],
[0,0,0,4,1,9,0,0,5],
[0,0,0,0,8,0,0,7,9]]
sudoku24=[[0,0,0,0,0,0,0,0,0], ###not solvable
[2,0,0,0,0,3,0,8,5],
[0,0,1,0,2,0,0,0,0],
[0,0,0,5,0,7,0,0,0],
[0,0,4,0,0,0,1,0,0],
[0,9,0,0,0,0,0,0,0],
[5,0,0,0,0,0,0,7,3],
[0,0,2,0,1,0,0,0,0],
[0,0,0,0,4,0,0,0,9]]
sudoku25=[[0,0,0,3,7,0,0,2,0],
[0,9,0,0,8,5,7,0,0],
[3,0,0,9,0,0,0,0,5],
[1,0,0,0,0,0,0,8,0],
[0,0,0,0,0,0,3,0,0],
[0,0,0,0,9,0,0,0,7],
[2,0,0,6,0,0,0,0,1],
[0,4,8,0,0,0,6,0,0],
[0,3,1,0,0,0,0,4,0]]