我正在尝试用Python解决一个6x6数独问题

我正在尝试用Python解决一个6x6数独问题,python,sudoku,solution,Python,Sudoku,Solution,我就是不明白为什么我找不到解决办法? 有人能告诉我如何找到解决这个问题的正确方法吗 当我尝试输入数字1-6时,它不起作用。只需打印“无解决方案”,这样它就跳过了代码中的解决方案部分 非常感谢你 SIZE = 6 # sudoku problem # 0 is unknown cells board = [ [0, 6, 0, 0, 0, 0], [0, 0, 0, 6, 2, 4], [3, 0, 4, 0, 1, 0], [0, 0, 0, 2, 0,

我就是不明白为什么我找不到解决办法? 有人能告诉我如何找到解决这个问题的正确方法吗

当我尝试输入数字1-6时,它不起作用。只需打印“无解决方案”,这样它就跳过了代码中的解决方案部分

非常感谢你

    SIZE = 6
# sudoku problem
# 0 is unknown cells
board = [
    [0, 6, 0, 0, 0, 0],
    [0, 0, 0, 6, 2, 4],
    [3, 0, 4, 0, 1, 0],
    [0, 0, 0, 2, 0, 0],
    [0, 0, 0, 4, 5, 0],
    [0, 0, 1, 0, 0, 2]]


# function to print sudoku
def print_sudoku():
    for i in board:
        print(i)


# Function to check if cells are unassigned and to add value to unassigned cells.
def unassigned_numbers(row, col):
    unassigned_num: int = 0
    for i in range(0, SIZE):
        for j in range(0, SIZE):
            # cell is unassigned
            if board[i][j] == 0:
                row = i
                col = j
                num_unassign = 1
                a = [row, col, num_unassign]
                return a
    a = [-1, -1, unassigned_num]
    return a


# function to check if a number can be assign a specific cell
def is_safe(n, r, c):
    # checking row
    for i in range(0, SIZE):
        # Cell has the same value
        if board[r][i] == n:
            return False
    # checking column
    for i in range(0, SIZE):
        # Cell has the same value
        if board[i][c] == n:
            return False
    row_start = (r // 2) * 3;
    col_start = (c // 3) * 2;
    # checking box
    for i in range(row_start, row_start + 3):
        for j in range(col_start, col_start + 2):
            if board[i][j] == n:
                return False
    return True


# Function to  check if we can put a value to a given cell
def solve_sudoku():
    row = 0
    col = 0
    # if all cells are assigned then the sudoku is already solved
    # pass by reference because number_unassigned will change the values of row and col
    a = unassigned_numbers(row, col)
    if a[2] == 0:
        return True
    row = a[0]
    col = a[1]
    # number between 1 to 6
    for i in range(1, 7):
        if is_safe(i, row, col):
            board[row][col] = i
            # backtracking
            if solve_sudoku():
                return True
            # if the solution don't work reassign the cell
            board[row][col] = 0
    return False


if solve_sudoku():
    print_sudoku()
else:
    print("No solution")
这里有什么问题? 我猜可能是在这一部分:

检查列
希望有人能帮我:)你确定这些计算是正确的吗?以
r=6
为例,这将返回
6
row\u start
,将
3
添加到此索引将是不允许的。请记住,您要选中给定框中的所有数字

(r // 2) * 2
r -> row_start
1 -> 0 
2 -> 2 
3 -> 2
4 -> 4
5 -> 4
6 -> 6

(c // 3) * 2
c -> col_start
1 -> 0
2 -> 0
3 -> 2
4 -> 2
5 -> 2
6 -> 4

我得到了要运行的代码,但输出并不像预期的那样。似乎数独游戏中缺少了框,因为它只考虑行和列。有人能给我指出正确的方向,让数独游戏也为方块添加算法吗?如何在代码中实现这一点?尝试了这么多东西,但我被卡住了

我得到了要运行的代码,但输出并不像预期的那样。似乎数独游戏中缺少了框,因为它只考虑行和列。有人能给我指出正确的方向吗?如何在代码中实现这一点?尝试了这么多东西,但我被卡住了!
(r // 2) * 2
r -> row_start
1 -> 0 
2 -> 2 
3 -> 2
4 -> 4
5 -> 4
6 -> 6

(c // 3) * 2
c -> col_start
1 -> 0
2 -> 0
3 -> 2
4 -> 2
5 -> 2
6 -> 4