八皇后問題python解法

czl389發表於2020-11-01

八皇后問題python解法

遞迴解法,board 是一維列表,下標表示行,值表示皇后所在的列。

board = [-1] * 8


def printboard(result):
    for v in result:
        length = len(result)
        print('□ '*v + '■ ' + '□ '* (length-v-1))
    print('\n')

def is_valid(board, row, col):
    for r in range(row):
        if col == board[r] or abs(row - r) == abs(col - board[r]):
            return False
        continue
    return True

def eightQueen(board, row):
    if row >= len(board):
        printboard(board)
        return True

    for col in range(len(board)):
        if is_valid(board, row, col):
            board[row] = col
            if eightQueen(board, row+1):
                pass
                #return True
    return False

eightQueen(board, 0)

相關文章