Python数独求解器块

时间:2014-03-13 17:55:03

标签: python block sudoku solver

我的数独求解器有问题。 我能够检查垂直和水平,但现在我将检查块。 这是我现在用来检查块的代码

def print_sudoku(array):
    for i in range(0,3):
        print()
        for j in range(0,3):
            print(array[i][j][0],end=' ')
def controleren(array):
    for i in range(0,8):
        for j in range(0,8):
            if distance[i][j][1] + distance[i][j][2]+ distance[i][j][3]+ distance[i][j][4]+ distance[i][j][5]+ distance[i][j][6]+ distance[i][j][7]+ distance[i][j][8]+ distance[i][j][9] == 1:
                if distance[i][j][k] == 1:
                    distance[i][j][k] = k

def main():
    pass
import pprint
distance = [[[0 for k in range(10)] for j in range(3)] for i in range(3)]

distance[0][0][0] = '*'
distance[0][1][0] = 2
distance[0][2][0] = 3
distance[1][0][0] = 4
distance[1][1][0] = 5
distance[1][2][0] = 6
distance[2][0][0] = 7
distance[2][1][0] = 8
distance[2][2][0] = 9

print_sudoku(distance)

在这里,我将每种可能性赋予价值1

for i in range(0,3):
    for j in range(0,3):
        if distance[i][j][0] == '*':
            for k in range(1,10):
                distance[i][j][k] = 1

这是重要的部分。 while循环是无限距离[0] [0] [0]保持等于*而不是得到值1,这是块中唯一丢失的数字。发生的事情是:已经站在其中一个距离[] [] []中的每个值都设置为0

while distance[0][0][0] == '*':
    for i in range(0,3):
        for j in range(0,3):
            if distance[i][j][0] != '*':
                k = distance[i][j][0]
                for i in range(0,3):
                    for j in range(0,3):
                        distance[i][j][k] = 0

在这部分中,代码会查找缺失的数字,因此有一个可能,一个数值为1,因为该数字是缺失的数字。

    for i in range(0,3):
        for j in range(0,3):
            if distance[i][j][1] + distance[i][j][2]+ distance[i][j][3]+ distance[i][j][4]+ distance[i][j][5]+ distance[i][j][6]+ distance[i][j][7]+ distance[i][j][8]+ distance[i][j][9] == 1:
                for k in range(1,10):
                    if distance[i][j][k] == 1:
                        distance[i][j][0] = k

print('')
print_sudoku(distance)

我希望你能理解它,可能不是(我觉得很难解释我在做什么)所以如果有些事情不清楚就说出来。

鲁迪

1 个答案:

答案 0 :(得分:0)

while distance[0][0][0] == '*':
    for i in range(0,3):
        for j in range(0,3):
            if distance[i][j][0] != '*':
                k = distance[i][j][0]
                for i in range(0,3):
                    for j in range(0,3):
                        distance[i][j][k] = 0

这里有两个ij。我认为这是导致问题的部分。尝试重命名它们,因为它会影响输出。

所以底部应该是这样的。

 k = distance[i][j][0]
 for m in range(0,3):
     for n in range(0,3):
         distance[m][n][k] = 0

这是你的代码。

distance = [[[0 for k in range(10)] for j in range(3)] for i in range(3)]

distance[0][0][0] = '*'
distance[0][1][0] = 2
distance[0][2][0] = 3
distance[1][0][0] = 4
distance[1][1][0] = 5
distance[1][2][0] = 6
distance[2][0][0] = 7
distance[2][1][0] = 8
distance[2][2][0] = 9

for i in range(0,3):
    for j in range(0,3):
        if distance[i][j][0] == '*':
            for k in range(1,10):
                distance[i][j][k] = 1

if distance[0][0][0] == '*':
    for i in range(0,3):
        for j in range(0,3):
            if distance[i][j][0] != '*':
                k = distance[i][j][0]
                for i in range(0,3):
                    for j in range(0,3):
                        distance[i][j][k] = 0
print (distance)

当存在相同的迭代变量时:

>>>[[['*', 1, 0, 1, 0, 1, 1, 0, 0, 0], ... ]

重命名后:

>>>[[['*', 1, 0, 0, 0, 0, 0, 0, 0, 0], ...]

我将while更改为if,以查看distance中的更改。