I'm studying the theory of algorithms and their possible resolutions methods.In this case I have some problems with backtracking. I want to write a function that fill a sudoku. But it doesn't print anything. Where is the error? The function defsett(i,j) take as input two numbers that are the coordinates of the number selected and set two integer (w and o) that are the starting points of the section 3x3 of the input number. Instead the sudoku function recursively tries to fill the matrix with the rules of sudoku.
# defsett = returns the coordinates of the first cell of the section 3x3
# where is the cell [i,j]
def defsett(i,j):
if(0<=i<=2):
if(0<=j<=2):
return (0,0)
elif(3<=j<=5):
return (0,3)
elif(6<=j<=8):
return (0,6)
elif(3<=i<=5):
if(0<=j<=2):
return (3,0)
elif(3<=j<=5):
return (3,3)
elif(6<=j<=8):
return (3,6)
else:
if(0<=j<=2):
return (6,0)
elif(3<=j<=5):
return (6,3)
elif(6<=j<=8):
return (6,6)
M = [[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]]
# n = matrix side
# i = index of rows
# j = index of cols
def sudoku(n,i,j,M):
if(i==n):
print(M)
elif(j==n):
j=0
i=i+1
sudoku(n,i,j,M)
else:
if(M[i][j]==0):
for number in range(1,n+1):
xInRows = False
xInCols = False
xInSection = False
# checking if number already present in this row
for k in range(n):
if (number == M[i][k]):
xInRows = True
# checking if number already present in this cols
for k in range(n):
if(number == M[k][j]):
xInCols = True
w,o=defsett(i,j) # first cell of this section
# checking if number already present in this section 3x3
for t in range(w,w+3):
for b in range(o,o+3):
if(number == M[t][b]):
xInSection = True
if(not(xInRows) and not(xInCols) and not(xInSection)):
M[i][j] = x
sudoku(n,i,j+1,M)
else:
sudoku(n,i,j+1,M)
sudoku(9,0,0,M)
-----
For this input works:
M = [[5,3,0,0,7,0,0,0,0],
[6,0,0,1,9,5,0,4,8],
[1,9,8,3,4,2,5,6,7],
[8,5,9,7,6,1,4,2,3],
[4,2,6,8,5,3,7,9,1],
[7,1,3,9,2,4,8,5,6],
[9,6,1,5,3,7,2,8,4],
[2,8,7,4,1,9,6,3,5],
[3,4,5,0,8,6,1,7,9]]
For this doesn't:
M = [[5,3,0,0,7,0,0,0,0],
[6,0,0,1,9,5,0,0,8],
[1,9,8,3,4,2,5,6,7],
[8,5,9,7,6,1,4,2,3],
[4,2,6,8,5,3,7,9,1],
[7,1,3,9,2,4,8,5,6],
[9,6,1,5,3,7,2,8,4],
[2,8,7,4,1,9,6,3,5],
[3,4,5,0,8,6,1,7,9]]