python - 回溯在 Python 中的工作原理

标签 python backtracking sudoku

这段代码是我从Youtube上学来的。我用它通过执行回溯来解决数独问题。

import pandas as pd
import numpy as np


raw = pd.read_csv(r'C:\Users\Administrator\Dropbox\Python_Learning\debaisudoku.csv', header = None)
sudoku = np.nan_to_num(raw)

def possible(x,y,n):
    # global sudoku
    for i in range(0,9):
        if sudoku[i][y] == n:
            return False
    for i in range(0,9):
        if sudoku[x][i] == n:
            return False
        x0 = (x//3) * 3
        y0 = (y//3) * 3
    for i in range(0,3):
        for j in range(0,3):
            if sudoku[x0+i][y0+j] == n:
                return False
    return True

def solve():
    # global sudoku
    for x in range(9):
        for y in range(9):
            if sudoku[x][y] == 0:
                for n in range(1,10):
                    if possible(x,y,n):
                        sudoku[x][y] = n
                        if solve(): return True
                    sudoku[x][y] = 0
                return False
    print(sudoku)
solve()

一切都很好,我理解除了这些代码行之外的代码:

    if possible(x,y,n):
        sudoku[x][y] = n
        if solve(): return True
    sudoku[x][y] = 0
return False

Python 如何运行、循环并记住位置然后继续计算上次使用的数字?顺便说一下,如果可能的话,请告诉我如何在 VBA 中执行回溯。我试过 goto with if condition 但没有任何效果。

非常感谢,我很感激任何答案。

最佳答案

在我看到 youtube 上的计算机爱好者剧集后,我也一直在 VBA 中尝试它。

我想如果你想在 VBA 中“返回”,你需要使用“退出函数”功能。

当我在 Excel 工作表中使用前 9*9 个单元格作为网格时,这段代码对我有用,在确认消息框后,数独将自行重置,我还不知道为什么会这样。

如果有人知道更简洁的编码方式,我会很高兴知道,希望这对您有所帮助!

Function possible(y, x, n) As Boolean
    For i = 1 To 9
        If Cells(y, i) = n Then
        possible = False
        Exit Function
        End If
    Next i
    For i = 1 To 9
        If Cells(i, x) = n Then
        possible = False
        Exit Function
        End If
    Next i

x0 = ((x - 1) \ 3) * 3
y0 = ((y - 1) \ 3) * 3
For i = 1 To 3
   For j = 1 To 3
    If Cells(y0 + i, x0 + j) = n Then
    possible = False
    Exit Function
    End If
    Next j
  Next i
possible = True

End Function

Function solve()


For y = 1 To 9
    For x = 1 To 9
        If Cells(y, x).Value = 0 Then
            For n = 1 To 10
                Debug.Print (n)
                If n = 10 Then
                    Exit Function
                End If
                    If possible(y, x, n) = True Then
                        Cells(y, x).Value = n
                        solve
                        Cells(y, x).Value = 0
                    End If
            Next n
        End If
    Next x
Next y

MsgBox ("solved!")

End Function

Sub solve_sudoku()
solve
End Sub 

关于python - 回溯在 Python 中的工作原理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60372254/

相关文章:

python - re模块无法找到正斜杠后面的字符串

python - Pandas :获取系列对象中的值(value)标签

python - 从字典列表中查找最小键值,忽略 None 值

c++ - 查看一步步执行

python - 在 PyCharm 中调用 unittest.main() - AtrributeError : module '__main__' has no atrribute

algorithm - 创建 nxn 矩阵 - 数独类

Prolog 剪切行为对我来说没有意义

java - 将Matlab程序转换为Java以提高性能的敏感性

java - 逻辑求解算法(Java 数独)

python - (Python) 检查数独中的 3x3,有更好的方法吗?