问题描述
我正在编写一个数独谜题求解器,它将列表列表作为“谜题”,每个列表代表谜题中的一行。我创建了一个包含多个函数来处理谜题的脚本,但是当我尝试解决谜题时遇到了递归错误。我相信 solve() 函数存在问题,但 get_question() 函数也可能存在问题,尤其是我处理列的方式。如果需要,我可以附加其他功能。
'''
def get_options(puzzle,row,col):
if puzzle[row][col] > 0:
return None
used = []
for item in puzzle[row]:
if item != 0:
used.append(item)
for i in range(len(puzzle)):
if puzzle[i][col] != 0:
used.append(item)
start_row = 3 * int(row / 3)
start_col = 3 * int(col / 3)
for x in range(3):
if puzzle[start_row + x] != 0:
used.append(puzzle[start_row + x])
for i in range(3):
if puzzle[start_row + x][start_col + i] != 0:
used.append(puzzle[start_row + x][start_col + i])
options = []
for i in range(10):
if i not in used:
options.append(i)
return options
def solve(puzzle,row=0,col=0):
if puzzle[row][col] > 0:
next_row,next_col = get_next(row,col)
if next_row is None:
return puzzle
# issue here
else:
solve(puzzle,next_row,next_col)
else:
options = get_options(puzzle,col)
if not options:
return None
else:
for opt in options:
new_puzzle = copy_puzzle(puzzle)
new_puzzle[row][col] = opt
# issue here
result = solve(new_puzzle)
if result is not None:
return result
'''
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)