Python数独求解器不返回解决方案

时间:2013-09-29 07:45:15

标签: python sudoku

我一直在编写这个程序,在继续之前测试每个部分的工作情况。然而,当我完成并把所有东西放在一起时,我无法得到解决方案。我试图通过为每个方块创建一个可能的数字列表来解决数独,并根据现有的方块移除它们。我假设当一个正方形中只有一个可能的数字时,这就是解决方案。它会循环,直到它完成。

我一直在查看我的代码半小时但仍然没有运气。我插入raw_input("")以查看是否有任何问题。我发现它在开始时取得了一些进展,但随后又停止了。

所以我打印了一个坐标的可能数字以及过程中的某个地方,每个可能性都被删除了。

以下是代码的样子:

# Create the Sodoku grid
grid = [[3,2,0,1,6,0,8,0,9],
        [0,7,8,9,0,3,1,2,6],
        [6,0,0,8,0,0,4,5,3],
        [7,1,0,4,0,0,0,6,2],
        [5,4,0,0,0,0,0,0,7],
        [0,0,0,2,0,5,3,1,0],
        [0,5,9,7,4,0,2,0,8],
        [2,0,7,5,0,9,0,0,0],
        [8,6,4,0,0,0,0,9,5],]

# Create possibilities
possible = {}
for y in range(9):
    for x in range(9):
        possible[(y,x)] = [1,2,3,4,5,6,7,8,9]

# A function that returns the row it is in.
def check_row(y,x):
    return grid[y]

# A function that returns the column it is in.
def check_column(y,x):
    column = []
    for hops in range(9):
        column.append(grid[hops][x])
    return column

# A function that returns the square it is in.
#  ------------- 
# 1| 0 | 1 | 2 |
#  -------------
# 2| 3 | 4 | 5 |
#  -------------
# 3| 6 | 7 | 8 |
#  -------------
#    1   2   3
def check_square(they,thex):

    square0 = []
    square1 = []
    square2 = []
    square3 = []
    square4 = []
    square5 = []
    square6 = []
    square7 = []
    square8 = []

    for y in range(3):
        for x in range(3):
             square0.append([y,x])

    for y in range(3):
        for x in range(3,6):
            square1.append([y,x])

    for y in range(3):
        for x in range(6,9):
            square2.append([y,x])

    for y in range(3,6):
        for x in range(3):
            square3.append([y,x])

    for y in range(3,6):
        for x in range(3,6):
            square4.append([y,x])

    for y in range(3,6):
        for x in range(6,9):
            square5.append([y,x])

    for y in range(6,9):
        for x in range(3):
            square6.append([y,x])

    for y in range(6,9):
        for x in range(3,6):
            square7.append([y,x])

    for y in range(6,9):
        for x in range(6,9):
            square8.append([y,x])

    tests = [square0,
             square1,
             square2,
             square3,
             square4,
             square5,
             square6,
             square7,
             square8]

    square_list = []

    def list_of_grid(result):
        for cood in result:
            [they,thex] = cood
            square_list.append(grid[they][thex])


    # Check which square it of and print the list of grid
    for test in tests:
        if [they,thex] in test:
            list_of_grid(test)

    return square_list


# Function that eliminates row possibilities
def elim_row(y, x):

    get_rid_of = []
    for element in check_row(y, x):
        if element != 0:
            get_rid_of.append(element)

    for stuff in get_rid_of:
        try:
            if stuff in possible[(y,x)]:
                possible[(y,x)] = []
            else:
                possible[(y,x)].remove(stuff)
        except ValueError:
            pass

# Funciton that eliminates column possibilites
def elim_column(y, x):

    get_rid_of = []
    for element in check_column(y, x):
        if element != 0:
            get_rid_of.append(element)

    for stuff in get_rid_of:
        try:
            if stuff in possible[(y,x)]:
                possible[(y,x)] = []
            else:
                possible[(y,x)].remove(stuff)
        except ValueError:
            pass

# Function that eliminates square possibilites
def elim_square(y, x):

    get_rid_of = []
    for element in check_square(y, x):
        if element != 0:
            get_rid_of.append(element)

    for stuff in get_rid_of:
        try:
            if stuff in possible[(y,x)]:
                possible[(y,x)] = []
            else:
                possible[(y,x)].remove(stuff)     
        except ValueError:
            pass

# Check if done:
def done():
    empty = 0
    for y in range(9):
        for x in range(9):
            if grid[y][x] == 0:
                empty += 1
    if empty == 0:
        return True
    else:
        return False

# print grid
if __name__ == "__main__":
    # Go through each row, column and square and delete possibilites
    while done != True:

        raw_input("")

        for cood in possible.keys():
            (y, x) = cood

            elim_row(y,x)
            elim_column(y,x)
            elim_square(y,x)

            # Check if len of possible == 1
            if len(possible[cood]) == 1:
                grid[y][x] = possible[cood][0]

        print possible[(0,2)]
        for rows in grid:
            print rows

1 个答案:

答案 0 :(得分:1)

您永远不会致电 done()。您只测试函数对象是否永远不等于True

while done != True:

函数对象永远不会等于True。不要在这里测试相等性,只需调用函数:

while not done():

接下来,在elim_row()中,只要您循环覆盖值,就可以清除可能的值:

for stuff in get_rid_of:
    try:
        if stuff in possible[(y,x)]:
            possible[(y,x)] = []
        else:
            possible[(y,x)].remove(stuff)
    except ValueError:
        pass

possible[(y,x)]设置为行中任何值的空值而不是0.您在其他2个elim_函数中执行相同操作。

您可能想要使用:

for stuff in get_rid_of:
    if stuff in possible[(y,x)]:
        possible[(y,x)].remove(stuff)

这将很快清除你的可能性。