Python - Sudoku Solver在给定特定值的情况下陷入无限循环

时间:2016-06-08 22:26:59

标签: python loops infinite sudoku

这是我的数独求解器的代码。最后,我将某些值放在某些方格中。当我包括除最后一个之外的所有值时,解算器工作。当我包含最后一个(注释掉的)值时,求解器进入无限循环。有人能够明白这是为什么吗?

class Cell:
    def __init__(self, value, isPermanent):
        self.value = 0
        self.isPermanent = False


def solve(board):  # solves the board
    if valid(board) is False:
        print("This board is unsolvable.")
        return
    row = 0  # begin in the first cell in the first row
    column = 0
    while row != 9:  # while there are still empty spaces in the 9 rows,
        printBoard(board)
        print(), print()
        if row <= 8 and column <= 8:
            row, column = add1(board, row, column)  # add 1 to the current cell
        if valid(board) is True:  # if the board is valid,
            if column == 8:  # if in the last cell of a row,
                row += 1  # move up one row, and begin in the first cell
                column = 0
            else:  # otherwise, move on to the next cell
                column += 1
            continue  # restart
        if valid(board) is False:  # if the board is invalid,
            if board[row][column].value == 9:  # if the value of the current cell is equal to 9,
                board[row][column].value = 0  # set it equal to 0
                if column == 0:  # if in the first cell of a row,
                    row -= 1  # go back a row, and into the last cell
                    column = 8
                else:  # otherwise, move back to the previous cell
                    column -= 1
            continue  # restart


def add1(board, row, column):  # increments each cell
    while row != 9:
        if board[row][column].isPermanent:
            if column == 8:
                row += 1
                column = 0
            else:
                column += 1
            continue
        if board[row][column].value == 9:  # if the value of the cell is equal to 9,
            board[row][column].value = 0  # set it equal to 0
            if column == 0:  # if in the first cell of a row,
                row -= 1  # go back a row, to the last cell
                column = 8
            else:  # if not in the first cell,
                column -= 1  # go to the previous cell
        board[row][column].value += 1  # add 1 to the current cell
        return row, column  # return the new coordinate of the current cell
    return row, column


def valid(board):
    for i in range(1, 10):
        if checkRowsForDuplicate(board, i) is False:
            return False
        if checkColumnsForDuplicate(board, i) is False:
            return False
        if checkZonesForDuplicate(board, i) is False:
            return False
    return True


def checkRowsForDuplicate(board, x):
    for row in board:
        line = []
        for cell in row:
            line.append(cell.value)
        if line.count(x) > 1:
            return False


def checkColumnsForDuplicate(board, x):
    for i in range(0, 9):
        column = []
        for row in board:
            column.append(row[i].value)
        if column.count(x) > 1:
            return False


def checkZonesForDuplicate(board, x):
    y = [0, 3, 6]
    z = [3, 6, 9]
    for i in range(3):
        for j in range(3):
            if checkSingleZone(board, x, y[i], z[i], y[j], z[j]) is False:
                return False
    return True


def checkSingleZone(board, x, rowStart, rowEnd, columnStart, columnEnd):
    zoneValues = []
    for row in board[rowStart:rowEnd]:
        for column in row[columnStart:columnEnd]:
            zoneValues.append(column.value)
    if zoneValues.count(x) > 1:
        return False


def printBoard(board):
    for row in board:
        line = []
        for cell in row:
            line.append(cell.value)
        print(line)


def initializeBoard():
    board = [[], [], [], [], [], [], [], [], []]
    for row in board:
        for i in range(0, 9):
            row.append(Cell(0, False))
    return board


board = initializeBoard()

board[0][7].value = 2
board[0][7].isPermanent = True

board[2][1].value = 9
board[2][1].isPermanent = True

board[2][2].value = 8
board[2][2].isPermanent = True

board[4][4].value = 8
board[4][4].isPermanent = True

board[7][1].value = 5
board[7][1].isPermanent = True

board[8][8].value = 3
board[8][8].isPermanent = True

# before this value is entered, the board can be solved, 
# but trying to solve it with this value included results in an infinite loop
# board[7][4].value = 7
# board[7][4].isPermanent = True


solve(board)

1 个答案:

答案 0 :(得分:0)

我认为最后一次继续需要缩进