python - 为什么控制台卡在我用 python 解决 9X9 数独板的最后一个函数上?

标签 python arrays function sudoku

我正在尝试编写一些代码来用 python 解决一个简单的 9X9 数独板问题。 卡在最后一个函数上:solve(board)。

我的代码:

def check1D(A):
    miss = []
    for j in list(range(1,len(A)+1)):
        if j in A:
            continue
        else:
            miss.append(j)
    return(miss)

def check2D(B):
    return(check1D(B.flatten()))

def checkRow(board,x,y):
    return(check1D(board[x,:]))

def checkCol(board,x,y):
    return(check1D(board[:,y]))

def checkBox(board,x,y):
    ymin = (y//3)*3
    xmin = (x//3)*3
    return(check2D(board[xmin:xmin+3,ymin:ymin+3]))

上面的函数检查一维列表。 2D 展平数组以应用 1D 检查。 Row、col 使用一维检查来查找候选者。 Box 计算出右侧 3X3 所需的最小/最大,以使用 2D 函数检查缺失的数字。

def cand(board,x,y):
    list = []
    box = checkBox(board,x,y)
    row = checkRow(board,x,y)
    col = checkCol(board,x,y)
    for i in box:
        if i in row and i in col:
            list.append(i)
    if len(list) > 1:
        list = [0]
    elif not list:
        list.append(0)
    return(list)

def solve(board):
    while board.min() == 0:
        row = len(board[0,:])
        col = len(board[:,0])
        for i in range(row):
            for j in range(col):
                if board[i][j] == 0:
                    unique = cand(board,i,j)
                    board[i][j] = unique[0]
    return(board)

我已经检查了我的所有功能,它们似乎可以工作。 cand(board,x,y) 产生一个唯一的候选者或列表中的零。

控制台卡在用于 2D 数组的 B.flatten() 方法上。

这是我正在尝试解决的简单数独 9X9:

easy = np.array([
        [0,0,0,  3,7,0,  0,5,6],
        [5,0,0,  0,1,0,  9,7,0],
        [0,6,0,  9,8,0,  3,4,0],

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

        [0,7,5,  0,6,9,  0,2,0],
        [0,4,8,  0,2,0,  0,0,5],
        [2,9,0,  0,5,8,  0,0,0]
        ])

感谢任何帮助。想了解我最后一个函数中的循环有什么问题。 谢谢。

最佳答案

仅用该方法无法解决您的示例。它陷入了无限循环,无法找到更多唯一值。

卡住之前的最后一 block 板是:

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

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

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

easy 中定义的数据对于数独规则无效(即同一框/行/列中存在多个相同的值,例如中心框中有 3 个)。

这个答案的其余部分使用以下内容来弥补这一点:

easy = np.array([
        [0,0,0,  3,7,0,  0,5,6],
        [5,0,0,  0,1,0,  9,7,0],
        [0,6,0,  9,8,0,  3,4,0],

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

        [0,7,5,  0,6,9,  0,2,0],
        [0,4,8,  0,2,0,  0,0,5],
        [2,9,0,  0,5,8,  0,0,0]
        ])

除了 cand 中的唯一编号检查之外,您还可以添加一些检查来查看是否只有一个值能够进入该行/列/框中:

def cand(board,x,y):
    list = []
    box = checkBox(board,x,y)
    row = checkRow(board,x,y)
    col = checkCol(board,x,y)

    if len(box) == 1:
        return box

    if len(row) == 1:
        return row

    if len(col) == 1:
        return col

    for i in box:
        if i in row and i in col:
            list.append(i)
    if len(list) > 1:
        list = [0]
    elif not list:
        list.append(0)
    return(list)

这解决了董事会:

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

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

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

您可能还应该检查 solve 中的循环是否在未更改任何值的情况下完成,以处理程序陷入无限循环的情况。

关于python - 为什么控制台卡在我用 python 解决 9X9 数独板的最后一个函数上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59419106/

相关文章:

python - 在 python 中获取 N-many 列表的交集

arrays - [1 2 .. N] 排列的最长递增子序列

c - 内部有其他结构数组的结构的可用内存 - C

javascript - 如何将值从 Controller 外部传递到作用域?

php - 如何访问$Children页面 Controller 功能?

python - 如何清理元组并将其插入 csv 文件中?

python 字符串标记化 - 自定义词法分析器?

c - 为什么 GCC 保留空函数?

python - 使用简单程序进行字符串比较时出现意外的打印输出

c++ - 从其行和列的给定总和生成一个二维整数数组