微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

递归数独解谜错误

如何解决递归数独解谜错误

我正在编写一个数独谜题求解器,它将列表列表作为“谜题”,每个列表代表谜题中的一行。我创建了一个包含多个函数来处理谜题的脚本,但是当我尝试解决谜题时遇到了递归错误。我相信 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 举报,一经查实,本站将立刻删除。