【发布时间】:2020-06-13 12:52:04
【问题描述】:
我已经正确地制作了一个数独回溯求解器。但现在我需要得到解决方案,但由于某种原因,我不断得到原来的数独网格。数独问题有一个独特的解决方案。
answer = []
count = 0
def solution(sudoku):
global answer
global count
for y in range(9):
for x in range(9):
if sudoku[y][x] == 0: # Found an empty grid
for n in range(1, 10):
if isPossible(x, y, n, sudoku):
sudoku[y][x] = n
solution(sudoku) # Recursion here
sudoku[y][x] = 0 # If no solution then reset the grid and go back
solved = False
return sudoku
print("hi look at me")
if count == 0:
answer = list(sudoku)
print(answer)
count += 1
return sudoku
hi = solution(board)
print(answer)
print(hi)
在此解决方案中, answer = list(sudoku) 将解决方案放置到我的全局变量中,还请注意,我使用 list() 来确保当数独参数更改时全局答案不会更改。我已确保这只发生一次。我知道这种回溯将导致原始数独板,因为一旦解决了解决方案,该算法将设置 sudoku[y][x] = 0,有效地将解决方案归零。但是唯一的解决方案已经存储在答案中,当我尝试在函数之外打印答案时,我得到了原始未解决的数独板。 那么为什么函数中的“答案”得到了正确的解决方案,但是一旦函数之外的“答案”给了我原来的板? answer = list(sudoku) 只出现一次, list() 给了我一个新的列表对象,所以即使数独改变答案也永远不会改变,那么改变之后怎么办?经过数小时的调试,我真的被卡住了。如何将解决方案放入函数之外的内容中?
完整代码以防万一
board = [
[0, 0, 0, 2, 6, 0, 7, 0, 1],
[6, 8, 0, 0, 7, 0, 0, 9, 0],
[1, 9, 0, 0, 0, 4, 5, 0, 0],
[8, 2, 0, 1, 0, 0, 0, 4, 0],
[0, 0, 4, 6, 0, 2, 9, 0, 0],
[0, 5, 0, 0, 0, 3, 0, 2, 8],
[0, 0, 9, 3, 0, 0, 0, 7, 4],
[0, 4, 0, 0, 5, 0, 0, 3, 6],
[7, 0, 3, 0, 1, 8, 0, 0, 0]
]
def isPossible(x, y, n, sudoku):
if n in sudoku[y]: # Check if in row
return False
for i in range(0, 9):
if n == sudoku[i][x]: # Check if in column
return False
y //= 3
x //= 3
for i in range(y*3, y*3 + 3): # Check if in squares
for j in range(x*3, x*3 + 3):
if n == sudoku[i][j]:
return False
return True
answer = []
count = 0
def solution(sudoku):
global answer
global count
for y in range(9):
for x in range(9):
if sudoku[y][x] == 0: # Found an empty grid
for n in range(1, 10):
if isPossible(x, y, n, sudoku):
sudoku[y][x] = n
solution(sudoku) # Recursion here
sudoku[y][x] = 0 # If no solution then reset the grid and go back
return sudoku
print("hi look at me")
if count == 0:
answer = list(sudoku)
print(answer)
count += 1
return sudoku
hi = solution(board)
print(answer)
print(hi)
【问题讨论】: