Reputation: 33
I started working on a sudoku solver using backtracking and recursion. I am unable to print the solved Sudoku. I have tested the possible(y,x,n)
method and it works. The program finishes with Process finished with exit code 0
but without printing out the solved sudoku puzzle. I'm using python 3.7 with PyCharm Community Edition 2020.1.3 as my IDE.
import numpy as np
grid = [[5, 3, 0, 0, 7, 0, 0, 0, 0],
[6, 0, 0, 1, 5, 9, 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]]
def possible(y, x, n):
global grid
for i in range(9):
if grid[y][i] == n:
return False
for i in range(9):
if grid[x][i] == n:
return False
x0 = (x // 3) * 3
y0 = (y // 3) * 3
for i in range(3):
for j in range(3):
if grid[y0 + i][x0 + j] == n:
return False
return True
def solve():
global grid
for y in range(9):
for x in range(9):
if grid[y][x] == 0:
for n in range(1, 10):
if possible(y, x, n):
grid[y][x] = n
solve()
grid[y][x] = 0
return
print(np.matrix(grid))
if __name__ == "__main__":
solve()
Upvotes: 0
Views: 130
Reputation: 41872
I have tested the possible(y,x,n) method and it works.
But it's broken:
if grid[x][i] == n:
Should be:
if grid[i][x] == n:
Next issue is the puzzle you're trying to solve, is broken! The sixth column has two nines in it:
[., ., ., ., ., 0, ., ., .]
[., ., ., ., ., 9, ., ., .]
[., ., ., ., ., 0, ., ., .]
[., ., ., ., ., 0, ., ., .]
[., ., ., ., ., 3, ., ., .]
[., ., ., ., ., 0, ., ., .]
[., ., ., ., ., 0, ., ., .]
[., ., ., ., ., 9, ., ., .]
[., ., ., ., ., 0, ., ., .]
You might want to add a puzzle verifier to your set of functions. In my example below, I use a different puzzle that's solvable, as it's hard to debug your code otherwise!
Finally, your solve()
function is underwritten. It shouldn't print the puzzle, but rather return a boolean indicating whether it solved the puzzle or not. This result is then used in your backtracking, and at the end to determine if the puzzle is solvable or not.
Finally, read up on the global
keyword, you're using it incorrectly.
import numpy as np
def possible(y, x, n):
for i in range(9):
if grid[y][i] == n:
return False
for i in range(9):
if grid[i][x] == n:
return False
x0 = (x // 3) * 3
y0 = (y // 3) * 3
for i in range(3):
for j in range(3):
if grid[y0 + i][x0 + j] == n:
return False
return True
def solve():
for y in range(9):
for x in range(9):
if grid[y][x] == 0:
for n in range(1, 10):
if possible(y, x, n):
grid[y][x] = n # tentatively try n
solved = solve()
if solved:
return True # solved recursively!
grid[y][x] = 0 # undo attempt at n
return False # no solution for this square
return True # no 0's to resolve, puzzle solved!
if __name__ == "__main__":
grid = [
[6, 5, 8, 0, 0, 0, 0, 7, 0],
[0, 7, 0, 0, 5, 0, 8, 0, 0],
[0, 3, 9, 0, 0, 0, 5, 4, 0],
[0, 0, 2, 6, 0, 5, 0, 0, 7],
[0, 6, 0, 9, 7, 4, 0, 0, 0],
[7, 0, 0, 3, 0, 0, 6, 0, 0],
[0, 4, 6, 0, 0, 0, 2, 5, 0],
[0, 0, 7, 0, 6, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 7, 6, 8]
]
if solve():
print(np.matrix(grid))
else:
print("No solution!")
OUTPUT
> python3 test.py
[[6 5 8 1 4 3 9 7 2]
[4 7 1 2 5 9 8 3 6]
[2 3 9 7 8 6 5 4 1]
[3 9 2 6 1 5 4 8 7]
[8 6 5 9 7 4 1 2 3]
[7 1 4 3 2 8 6 9 5]
[1 4 6 8 3 7 2 5 9]
[9 8 7 5 6 2 3 1 4]
[5 2 3 4 9 1 7 6 8]]
>
Upvotes: 1