我正在尝试编写python程序来解决数独难题,并且正在使用回溯方法。我已经设法解决了难题,但是当我尝试返回已解决的数独时,它只返回了一个,但是可以打印数独。这是代码的主体,我得到的结果如下:
def sudoku_solver(sudoku_to_solve):
#solve using backtracking
sudoku = sudoku_to_solve
i = 0
j = 0
possiblities = {}
#check if sudoku is full
if isFull(sudoku) == True:
print("solved")
print(sudoku)
return sudoku
else:
#find first 0
for x in range (0, 9):
for y in range (0, 9):
if sudoku[x][y] == 0:
i = x
j = y
break
else:
continue
break
possiblities = possibleEntries(sudoku, i, j)
for x in range (1, 10):
if not possiblities[x] == 0:
sudoku[i][j] = possiblities[x]
sudoku_solver(sudoku)
# backtrack
sudoku[i][j] = 0
#return solved_sudoku
print(sudokus[0])
print(sudoku_solver(sudokus[0]))
[[0. 0. 4. 0. 8. 3. 0. 0. 2.]
[0. 5. 1. 0. 0. 4. 3. 0. 0.]
[0. 0. 0. 0. 9. 6. 7. 1. 0.]
[1. 2. 0. 8. 0. 0. 0. 0. 6.]
[0. 4. 0. 0. 0. 0. 5. 0. 0.]
[8. 3. 0. 6. 0. 7. 9. 0. 0.]
[0. 6. 0. 3. 0. 9. 0. 4. 0.]
[0. 0. 7. 0. 0. 0. 2. 0. 5.]
[0. 9. 0. 0. 5. 0. 8. 0. 3.]]
solved
[[9. 7. 4. 1. 8. 3. 6. 5. 2.]
[6. 5. 1. 2. 7. 4. 3. 8. 9.]
[2. 8. 3. 5. 9. 6. 7. 1. 4.]
[1. 2. 9. 8. 3. 5. 4. 7. 6.]
[7. 4. 6. 9. 1. 2. 5. 3. 8.]
[8. 3. 5. 6. 4. 7. 9. 2. 1.]
[5. 6. 8. 3. 2. 9. 1. 4. 7.]
[3. 1. 7. 4. 6. 8. 2. 9. 5.]
[4. 9. 2. 7. 5. 1. 8. 6. 3.]]
None
任何帮助将不胜感激