我的数独求解器完全按照它应该做的 - 除了返回正确的东西。它打印返回之前它应该正确的(正确解决的网格),但它似乎继续运行一点并返回None。我无法弄清楚发生了什么。
网格是一个列表列表。如果网格有效(已解决或未解决),则假设check_sudoku返回True,否则返回False。
def solve_sudoku(grid, row=0, col=0):
"Searches for valid numbers to put in blank cells until puzzle is solved."
# Sanity check
if not check_sudoku(grid):
return None
# Sudoku is solved
if row > 8:
return grid
# not a blank, try next cell
elif grid[row][col] != 0:
next_cell(grid, row, col)
else:
# try every number from 1-9 for current cell until one is valid
for n in range(1, 10):
grid[row][col] = n
if check_sudoku(grid):
next_cell(grid, row, col)
else:
# Sudoku is unsolvable at this point, clear cell and backtrack
grid[row][col] = 0
return
def next_cell(grid, row, col):
"Increments column if column is < 8 otherwise increments row"
return solve_sudoku(grid, row, col+1) if col < 8 else solve_sudoku(grid, row+1, 0)
答案 0 :(得分:3)
您在递归中调用next_cell
,但从不返回其值。
答案 1 :(得分:2)
在我看来,这似乎永远不会真正回归有用的东西。我们第一次输入您的solve_sudoku
时,您会检查网格是否已解决,如果是,则将其返回。之后,您开始一堆递归,最终将返回到函数末尾并返回None。无论如何,你一直都没有回归。你最终得到的唯一一件事是你传入的一个修改过的grid
参数。
def solve_sudoku(grid, row=0, col=0):
# possible return at the first entry point
if not check_sudoku(grid):
return None
# only time you would ever NOT get None
if row > 8:
return grid
...recurse...
# come back out when the stack unwinds,
# and there is no return value other than None
我猜测正在发生的事情是,你是在沿途打印这些值,并且在它发生的那一刻你正确地看到一个已解决的网格,但你的功能没有设置为正确地完全突破完成。它继续循环,直到耗尽一些范围,你会看到一堆额外的工作。
重要的是检查递归调用的返回值。如果未解决,您将返回None;如果未解决,则返回grid
。就目前而言,您从不关心在调用范围内递归的结果。
因为我没有关于您的特定代码的所有细节,所以这是一个简单的等价物:
def solver(aList):
if aList[0] == 10:
print "SOLVED!"
return None
print "NOT SOLVED..."
return next(aList)
def next(aList):
# do some stuff
# check some stuff
aList[0] += 1
return solver(aList)
if __name__ == "__main__":
data = [0]
solver(data)
print data
请注意,对checker() -> solver()
的间接递归调用将其值一直返回到链中。在这种情况下,我们说None
意味着解决了,否则它应该继续递归。您知道在递归堆栈的某个深处,解决方案将得到解决。您需要立即将其传达回顶部。
通讯如下:
aList == [0]
solver(aList)
next(...)
next(...)
next(...)
next(...)
#SOLVED
<- next(...)
<- next(...)
<- next(...)
<- next(...)
<-solver(aList)
aList == [10]
如果应用于我的简单示例,那么这就是您的版本的样子:
aList == [0]
solver(aList)
next(...)
next(...)
# SOLVED
next(...)
next(...)
...
<- next(...)
<- next(...)
<- next(...)
<- next(...)
<-solver(aList)
aList == [10]