简体   繁体   English

如何基于回溯修复数独求解器

[英]How to fix my sudoku solver based on backtracking

I tried to create a sudoku solver algorithm using python and based on backtracking but it always return nothing saying that the sudoku is incorrect. 我试图使用python并基于回溯来创建数独求解器算法,但它始终不返回任何内容来表示数独不正确。

Here is my code: 这是我的代码:

 board = [[3,0,6,5,0,8,4,0,0],
        [5,2,0,0,0,0,0,0,0],
        [0,8,7,0,0,0,0,3,1],
        [0,0,3,0,1,0,0,8,0],
        [9,0,0,8,6,3,0,0,5],
        [0,5,0,0,9,0,6,0,0],
        [1,3,0,0,0,0,2,5,0],
        [0,0,0,0,0,0,0,7,4],
        [0,0,5,2,0,6,3,0,0]]

e = [0, 0]

def checkEmpty():
    for i in range(0, 9):
        for j in range(0, 9):
            if board[i][j] == 0:
                e[0] = i
                e[1] = j
                return True
    return False

def vLine(i, test):
    for j in range(9):
        if board[i][j] == test:
            return True
    return False

def vColumn(j, test):
    for i in range(9):
        if board[i][j] == test:
            return True
    return False

def vBlock(i, j, test):
    for I in range(3):
        for J in range(3):
            if(board[i+I][j + J] == test):
                return True
    return False

def validMove(i, j, test):
    if vColumn(j, test) == False and vBlock(i, j, test) == False and vLine(i, test) == False:
        return True
    else:
        return False

def solve():
    e = [0, 0]
    if checkEmpty() == False:
        return True
    i = e[0]
    j = e[1]
    for k in range(1, 10):
        if validMove(i, j, k) == True:
            board[i][j] = k
            if solve() == True:
                return True
            board[i][j] = 0
    return False

if solve():
    print("solved!")
else:
    print("No solution exists")

The problem is that the code inside this if function if validMove(i, j, k) == True: seems to never be executed. 问题在于if函数中的代码if validMove(i, j, k) == True:似乎永远不会执行。 However I was unable to find any error in this function. 但是,我无法在此函数中找到任何错误。

In addition, I don't really know whether I should indent, dedent or keep this line board[i][j] = 0 here. 另外,我真的不知道该在这里缩进,缩进还是保留此线路board[i][j] = 0

Your block checker is wrong - if you input 2,2,9 it will not check the correct block but something misaligned. 您的块检查器是错误的-如果您输入2,2,9,它将不会检查正确的块,但会出现未对齐的情况。

def vBlock(i, j, test):
    for I in range(3):
        for J in range(3):
            if(board[i+I][j + J] == test):   # checks 2-5. 2-5 row&col
                return True
    return False

Change it to 更改为

def vBlock(i, j, test):
    for I in range(3):
        for J in range(3):
            if(board[i//3+I][j//3 + J] == test): # this way it cecks the blocks correctly 
                # if you input 2,2,9 it checks 2//3+range == 0+0-3 
                return True
    return False

This wont change the overall error, but compensate for one error at least. 这不会改变整体误差,但是至少补偿一个误差。


You recurse into solve() without ever changing the e -list of row/col currently checked - your solve() works on a local variable e - your checkEmpty() changes the global e , the changes are never reflected inside solve() . 您无需更改当前检查的行/列的e即可递归到solve() -您的solve()局部变量e -您的checkEmpty()更改了全局 e ,更改从未反映在solve()内部。

Fix: 固定:

def checkEmpty():
    global e        # explicitly use the global e
    for i in range(0, 9):
        for j in range(0, 9):
            if board[i][j] == 0:
                e[0] = i
                e[1] = j
                return True
    return False

def solve():
    global e        # explicitly use the global e
    if checkEmpty() == False:
        return True
    i = e[0]
    j = e[1]
    print(e)
    for k in range(1, 10): 
        if validMove(i, j, k) == True:
            board[i][j] = k
            if solve() == True:
                return True
            board[i][j] = 0

    return False

Even if you fix those 2 errors: 即使您修复了这两个错误:

You need to be able to discard ealier finds- fe at start one space might be able to be filled by 9,1,3,4 - you check 1 first - bingo - put it in, but run into problems later for the spot in this block that was only ever able to hold 1. Now 1 is given away and you have no solution. 您需要能够丢弃更安全的发现-在开始时一个空间可能可以被9,1、3、4填充-您首先检查1个-宾果游戏-将其放入,但稍后会遇到问题这个只能容纳1个的块。现在将1个分配给您,您将无解。

You need to calculate "all possible" numbers for all of your 0 first, then fill in those with only 1 possibility, removing that number from the corresponding row/column/block 0 possibilitie lists and continue until all are filled in. 您需要首先计算所有0 “所有可能”数字,然后填写只有1种可能性的数字,然后从相应的行/列/块0可能性列表中删除该数字,然后继续进行操作,直到所有数字都填满为止。

Bottom line: solving it using the first choice available might end you in a local minimun that solves 10 of 12 leftover zeros, and then you are stuck. 底线:使用第一选择方法求解可能会导致您获得解决12个剩余零中的10个的局部最小值,然后陷入困境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM