-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoku_solver_reversed.py
156 lines (133 loc) · 3.99 KB
/
sudoku_solver_reversed.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
def print_board(board):
print "+-------+-------+-------+"
for i in range(9):
string = "| "
for j in range(9):
if board[i][j] == 0:
string += ". "
else:
string += str(board[i][j]) + " "
if j in [2,5,8]:
string += "| "
print string
if i in [2,5,8]:
print "+-------+-------+-------+"
def is_full(board):
for x in range(0, 9):
for y in range (0, 9):
if board[y][x] == 0:
return False
return True
def check_col(board, x, y, num):
#print "\nchecking col......"
for i in range(9):
if i != y:
#print board[i][x],
if board[i][x] == num:
return False
return True
def check_row(board, x, y, num):
#print "\nchecking row......"
for i in range(9):
if i != x:
#print board[y][i],
if board[y][i] == num:
return False
return True
def check_box(board, x, y, num):
#print "\nchecking box......"
num_list = [[0,1,2],[3,4,5],[6,7,8]]
for i in num_list:
if x in i:
row = i
if y in i:
col = i
for i in row:
for j in col:
if not (i == x and j == y):
#print board[j][i],
if num == board[j][i]:
return False
return True
def check_all(board, x, y, num):
if check_row(board, x, y, num) and check_col(board, x, y, num) and check_box(board, x, y, num):
return True
return False
def fill(board, x, y):
possible = []
for i in range(1,10):
if check_all(board, x, y, i):
possible.append(i)
return possible[::-1]
board = [[0,4,0,6,3,9,0,5,0],\
[0,0,0,0,0,0,0,0,0],\
[0,9,3,0,0,0,2,6,0],\
[0,0,9,3,0,1,4,0,0],\
[0,0,8,0,0,0,7,0,0],\
[2,0,0,0,4,0,0,0,8],\
[0,2,0,9,0,4,0,3,0],\
[0,0,4,0,0,0,9,0,0],\
[9,0,0,2,0,3,0,0,5]]
boar1 = [[0,0,4,5,0,0,7,0,0],\
[0,2,0,0,9,0,0,4,0],\
[0,0,9,0,0,3,0,0,5],\
[0,3,0,0,4,0,8,0,0],\
[6,0,0,0,0,0,0,0,9],\
[0,4,0,0,2,0,3,0,0],\
[0,0,7,0,0,9,0,0,2],\
[0,9,0,0,8,0,0,1,0],\
[0,0,1,6,0,0,9,0,0]]
boar2 = [[2,0,0,0,0,0,8,0,0],\
[5,0,8,0,0,6,0,7,0],\
[0,4,0,0,8,9,0,0,6],\
[0,0,0,8,9,0,0,1,0],\
[0,8,4,0,0,0,0,0,0],\
[0,0,0,1,3,0,0,5,0],\
[0,3,0,0,5,8,0,0,1],\
[8,0,5,0,0,3,0,2,0],\
[4,0,0,0,0,0,3,0,0]]
boar3 = [[9,0,0,2,0,0,0,0,4],\
[0,4,0,0,9,0,0,0,0],\
[0,0,1,0,0,8,3,0,0],\
[0,2,0,0,0,5,7,0,0],\
[4,0,0,0,0,0,0,0,6],\
[0,0,3,7,0,0,0,8,0],\
[0,0,8,3,0,0,5,0,0],\
[0,0,0,0,1,0,0,9,0],\
[6,0,0,0,0,4,0,0,7]]
boar3 = [[5,0,0,0,0,0,0,0,7],\
[0,8,0,0,2,0,0,6,0],\
[0,0,4,3,0,0,1,0,0],\
[0,0,1,5,0,0,0,0,0],\
[0,9,0,0,0,0,0,2,0],\
[0,0,0,0,0,4,5,0,0],\
[0,0,7,0,0,1,4,0,0],\
[0,6,0,0,5,0,0,8,0],\
[2,0,0,0,0,0,0,0,9]]
print_board(boar3)
def solver(board):
i = 0
j = 0
# if board is full, there is no need to solve it any further
if is_full(board):
print("Board Solved Successfully!")
print_board(board)
return
else:
# find the first vacant spot
for x in range (0, 9):
for y in range (0, 9):
if board[x][y] == 0:
i = x
j = y
break
else:
continue
break
possible = fill(board, j, i)
if possible != []:
for num in possible:
board[i][j] = num
solver(board)
board[i][j] = 0
solver(boar3)