Sudoku求解器未返回完整的电路板

时间:2018-12-06 22:24:52

标签: python sudoku

import numpy as np




def row(board,x,y):
    """Takes in a board and x y coordinates and returns all
   the row candidates"""
    numbers = np.arange(1,10)
    missing = []
    for number in numbers:
        if number not in board[x]:
            missing.append(number)
        else:
            continue
    return missing

def column(board,x,y):
    """Takes an incomplete board and returns all the
   column candidates"""
    numbers = np.arange(1,10)
    missing = []
    for number in numbers:
        if number not in board[:,y]:
            missing.append(number)
        else:
            continue
    return missing

def box(board,x,y):
    """Takes an incomplete board, coordinates and returns the
   box wise candidates"""
    numbers = list(range(1,10))
    missing = []
    box1 = np.array([board[0,0:3],board[1,0:3],board[2,0:3]])
    box2 = np.array([board[0,3:6],board[1,3:6],board[2,3:6]])
    box3 = np.array([board[0,6:10],board[1,6:10],board[2,6:10]])
    box4 = np.array([board[3,0:3],board[4,0:3],board[5,0:3]])
    box5 = np.array([board[3,3:6],board[4,3:6],board[5,3:6]])
    box6 = np.array([board[3,6:10],board[4,6:10],board[5,6:10]])
    box7 = np.array([board[6,0:3],board[7,0:3],board[8,0:3]])
    box8 = np.array([board[6,3:6],board[7,3:6],board[8,3:6]])
    box9 = np.array([board[6,6:10],board[7,6:10],board[8,6:10]])

    condition1 = 0<=x<=2 and 0<=y<=2
    condition2 = 0<=x<=2 and 3<=y<=5
    condition3 = 0<=x<=2 and 6<=y<=8
    condition4 = 3<=x<=5 and 0<=y<=2
    condition5 = 3<=x<=5 and 3<=y<=5
    condition6 = 3<=x<=5 and 6<=y<=8
    condition7 = 6<=x<=8 and 0<=y<=2
    condition8 = 6<=x<=8 and 3<=y<=5
    condition9 = 6<=x<=8 and 6<=y<=8

    if condition1:
        for number in numbers:
            if number not in box1:
                missing.append(number)
            else:
                continue

    if condition2:
        for number in numbers:
            if number not in box2:
                missing.append(number)
            else:
                continue

    if condition3:
        for number in numbers:
            if number not in box3:
                missing.append(number)
            else:
                continue

    if condition4:
        for number in numbers:
            if number not in box4:
                missing.append(number)
            else:
                continue

    if condition5:
        for number in numbers:
            if number in box5:
                continue
            else:
                missing.append(number)

    if condition6:
        for number in numbers:
            if number not in box6:
                missing.append(number)
            else:
                continue

    if condition7:
        for number in numbers:
            if number not in box7:
                missing.append(number)
            else:
                continue

    if condition8:
        for number in numbers:
            if number not in box8:
                missing.append(number)
            else:
                continue

    if condition9:
        for number in numbers:
            if number not in box9:
                missing.append(number)
            else:
                continue

    return missing

def sudsolver(board):
    """Give it a board andt it will solve it for your using the functions defined above."""
    newboard = board.copy()
    for index,n in np.ndenumerate(board):
        if board[index] == 0:
            x = index[0]
            y = index[1]
            boxsolution = box(newboard,x,y)
            columnsolution = column(newboard,x,y)
            rowsolution = row(newboard,x,y)

            if len(boxsolution) or len(columnsolution) or len(rowsolution) < 1:
                for number in boxsolution:
                    if number in columnsolution and number in rowsolution:
                        newboard[index] = number
                    else:
                        continue

        else:
            continue

    return newboard

testboard = 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,   8, 0, 0,   2, 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]
])

pleasework = sudsolver(testboard)

我创建了不同的功能来检查给定单元格的解决方案,但是,当板从sudsolver函数返回时,板中仍然有几个0。

我正在使用的板称为测试板,我确信可以解决。

我想不出这是为什么以及如何解决它。任何帮助都会很棒。

0 个答案:

没有答案