Python中的全局变量重置

2024-10-02 00:29:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我用回溯算法(Python3.8)创建了一个数独解算器。这是一个递归算法。数独板(拼图)是一个全局变量(多个函数需要共享它)。solve()函数执行它的任务,但是即使在使用了global关键字之后,电路板的值也不会改变。需要你的帮助

守则:

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

def isPossible(y, x, val): # checks if it is legal to put a value at a certain position
    for row in board: # row condition
        if val == row[x]:
            return False

    for col in board[y]: # column condition
        if val == col:
            return False

    # subcell condition

    subCellRow = (y // 3) * 3
    subCellCol = (x // 3) * 3

    for row in board[subCellRow:subCellRow + 3]:
        for col in row[subCellCol:subCellCol + 3]:
            if val == col:
                return False

    return True

def solve():
    global board
    for y in range(9):
        for x in range(9):
            if board[y][x] == 0:
                for n in range(1, 10):
                    if isPossible(y, x, n): # python stairs
                        board[y][x] = n
                        solve() # recursion starts
                        board[y][x] = 0 # 1-line backtracking algorithm

                return

    printPuzzle() # prints the solved puzzle
    return True

def printPuzzle(): # to display the puzzle
    print()
    for row in board:
        for val in row:
            print(val, end = ' ')

        print()

printPuzzle()
solve()
printPuzzle() # prints the unsolved board

Tags: inboardfalseforreturnifdefcol
1条回答
网友
1楼 · 发布于 2024-10-02 00:29:51

这不是全局变量的错误,这是因为在算法的实现中有一个轻微的错误,在board[y][x] = 0处将值重新指定为零

这是我认为正确的实现方法

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


def isPossible(y, x, val): # checks if it is legal to put a value at a certain position
    for row in board: # row condition
        if val == row[x]:
            return False

    for col in board[y]: # column condition
        if val == col:
            return False

    # subcell condition

    subCellRow = (y // 3) * 3
    subCellCol = (x // 3) * 3

    for row in board[subCellRow:subCellRow + 3]:
        for col in row[subCellCol:subCellCol + 3]:
            if val == col:
                return False

    return True


def solve():
    global board
    for y in range(9):
        for x in range(9):
            if board[y][x] == 0:
                for n in range(1, 10):
                    if isPossible(y, x, n): # python stairs
                        board[y][x] = n
                        if solve():
                            return True # recursion starts
                        board[y][x] = 0
                return False

    printPuzzle() # prints the solved puzzle

    return True


def printPuzzle(): # to display the puzzle
    print()
    for row in board:
        for val in row:
            print(val, end = ' ')

        print()

printPuzzle()
solve()
printPuzzle() # prints the unsolved board

我得到的输出:

Program Output

相关问题 更多 >

    热门问题