为数独找到解决方案的回溯算法

时间:2019-06-26 14:38:43

标签: python python-3.x backtracking sudoku recursive-backtracking

我正在研究算法的理论及其可能的解决方法。在这种情况下,我遇到了回溯的问题。 我想编写一个填充数独的功能。但它不会打印任何内容。错误在哪里? 函数defsett(i,j)将两个数字作为所选数字的坐标作为输入,并设置两个整数(w和o)作为输入数字的3x3部分的起点。 取而代之的是sudoku函数尝试用sudoku规则填充矩阵。

# defsett = returns the coordinates of the first cell of the section 3x3 
# where is the cell [i,j]
def defsett(i,j):
    if(0<=i<=2):
        if(0<=j<=2):
            return (0,0)
        elif(3<=j<=5):
            return (0,3)
        elif(6<=j<=8):
            return (0,6)
    elif(3<=i<=5):
        if(0<=j<=2):
            return (3,0)
        elif(3<=j<=5):
            return (3,3)
        elif(6<=j<=8):
            return (3,6)
    else:
        if(0<=j<=2):
            return (6,0)
        elif(3<=j<=5):
            return (6,3)
        elif(6<=j<=8):
            return (6,6)
M = [[5,3,0,0,7,0,0,0,0],
     [6,0,0,1,9,5,0,0,0],
     [0,9,8,0,0,0,0,6,0],
     [8,0,0,0,6,0,0,0,3],
     [4,0,0,8,0,3,0,0,1],
     [7,0,0,0,2,0,0,0,6],
     [0,6,0,0,0,0,2,8,0],
     [0,0,0,4,1,9,0,0,5],
     [0,0,0,0,8,0,0,7,9]]

# n = matrix side
# i = index of rows
# j = index of cols

def sudoku(n,i,j,M):
        if(i==n):
            print(M)
        elif(j==n):
            j=0
            i=i+1
            sudoku(n,i,j,M)
        else:
            if(M[i][j]==0):
                for number in range(1,n+1):

                    xInRows = False
                    xInCols = False
                    xInSection = False

   # checking if number already present in this row 
                    for k in range(n): 
                        if (number == M[i][k]):
                            xInRows = True

   # checking if number already present in this cols 
                    for k in range(n):
                        if(number == M[k][j]):
                            xInCols = True

                    w,o=defsett(i,j) # first cell of this section

   # checking if number already present in this section 3x3
                    for t in range(w,w+3):
                        for b in range(o,o+3):
                            if(number == M[t][b]):
                                xInSection = True

                    if(not(xInRows) and not(xInCols) and not(xInSection)):
                        M[i][j] = x
                        sudoku(n,i,j+1,M)
            else:
                sudoku(n,i,j+1,M)
sudoku(9,0,0,M)
-----
For this input works:
M = [[5,3,0,0,7,0,0,0,0],
     [6,0,0,1,9,5,0,4,8],
     [1,9,8,3,4,2,5,6,7],
     [8,5,9,7,6,1,4,2,3],
     [4,2,6,8,5,3,7,9,1],
     [7,1,3,9,2,4,8,5,6],
     [9,6,1,5,3,7,2,8,4],
     [2,8,7,4,1,9,6,3,5],
     [3,4,5,0,8,6,1,7,9]]
For this doesn't:
M = [[5,3,0,0,7,0,0,0,0],
     [6,0,0,1,9,5,0,0,8],
     [1,9,8,3,4,2,5,6,7],
     [8,5,9,7,6,1,4,2,3],
     [4,2,6,8,5,3,7,9,1],
     [7,1,3,9,2,4,8,5,6],
     [9,6,1,5,3,7,2,8,4],
     [2,8,7,4,1,9,6,3,5],
     [3,4,5,0,8,6,1,7,9]]

1 个答案:

答案 0 :(得分:1)

什么都不打印的原因很简单:sudoku()进行得不够快,以至于i == 9都无法产生True。因此,让我们分析一下如何发生...

  1. i == 9⇒退出解决方案
  2. i != 9 and j == 9⇒递归到下一行; sudoku(9, i+1, 0, M)
  3. i != 9 and j != 9
    1. M[i][j] != 0⇒递归到下一列sudoku(9, i, j+1, M)
    2. M[i][j] == 0⇒尝试使用数字
      1. 找到了候选者number⇒设置了M[i][j] = number不是 M[i][j] = x)并进入下一列sudoku(9, i, j+1, M)
      2. 的递归

这就是整个决策过程。一旦M退出,这就是sudoku()

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

为了了解问题出在哪里,想象一下您在第八列中输入4后正在呼叫sudoku(9, 0, 8, M) ...

1是尚未在此行中放置的唯一数字,但是此处禁止使用它,因为它已经出现在第五行(M[4][8])中。 sudoku(9, 0, 8, M)退出,因为没有其他号码可以尝试。由于没有人能够将1放置在其他任何地方,因此控制从这里移到调用堆栈中。为什么1如此重要?因为所有其他数字已经放置在该行的某处,并且sudko()回溯时不会更改!

解决方法很简单:在递归步骤之后的一行中添加一个数字,该行将撤消更改以进行适当的回溯。

(...)
                    if(not(xInRows) and not(xInCols) and not(xInSection)):
                        M[i][j] = x
                        sudoku(n,i,j+1,M)
                        M[i][j] = 0
(...)

仅此一项就可以使sudoku()达到给定的M。但我不能保证它将为任何M找到解决方案。 ;-)