回溯法求解八皇后问题(python实现)

实验内容

八皇后问题是一个以国际象棋为背景的问题:如何能够在 8×8 的国际象棋棋 盘上放置八个皇后,使得任何一个皇后都无法直接吃掉其他的皇后。为了达 到此目的,任两个皇后都不能处于同一条横行、纵行或斜线上。

实验原理

按照 DFS 算法的策略,从跟结点出发搜索解空间树。首先根结点成为活结点, 同时也成为当前的扩展结点。在当前的扩展结点处,搜索向纵深方向移至一个新结点。这个新结点就成为新的活结点,并成为当前扩展结点。如果在当前的扩展结点处不能再向纵深方向移动,则当前扩展结点就成了死结点。此时应往回移动(回溯)至最近的一个活结点处,并使这个活结点成为当前的扩展结点。 回溯法以这种方式递归地在解空间中搜索,直到找到所要求的解或解空间中已无活结点为止。

实验步骤

数组法:(这里采用该方法实现)
① 根据 8 皇后问题,可以把其设想为一个数组;
② 根据 8 皇后的规则,可以设想为数组上同一直线,横线,斜线的数字都 不 能相同,由此可以得出判断条件;
③ 根据判断条件之后,建立回溯点,即可解决问题。
堆栈法:
① 检索当前行是否可以放置一个皇后;
② 利用检索过程,通过递归的方式,来确定每个皇后的位置———回溯的思想。

代码实现

import time

def show(queen):
    for i in range(0, 8):
        print("(" + str(i + 1) + ", " + str(queen[i] + 1) + ")", end = " ")
    print("\n")

def isNotConfict(queen, row):   #判断是否冲突
    for i in range(0, row):
        if queen[i] == queen[row] or abs(i - row) == abs(queen[i] - queen[row]):
            return False
    return True

sum = 0                         #记录解法总数

def put_queen(queen, row):      #回溯法放置皇后
    if row == len(queen):       #如果到达最后一行,那么肯定得到一种新解法
        show(queen)             #输出皇后位置
        global sum
        sum += 1                #更新解法总数
        return sum
    for column in range(0, len(queen)):#循环判断每列是否可以放置皇后
        queen[row] = column
        if isNotConfict(queen, row):
            put_queen(queen, row + 1)

def main():
    queen = [None] * 8
    start = time.time()
    put_queen(queen, 0)
    end = time.time()
    print("共耗时:\n" + str(end - start) + " s\n")
    print("一共有" + str(sum) + "种解法")


if __name__ == '__main__':
    main()

时间复杂度分析

因为回溯法遇到当前解不可行时就会回溯到上一结点,因此时间复杂度应该没有达到O(n!),看了很多大佬都说是O(n3),但是都没有求解过程,因此这里想请教各位访客如何求解该算法的时间复杂度。

运行结果

(1, 1) (2, 5) (3, 8) (4, 6) (5, 3) (6, 7) (7, 2) (8, 4) 

(1, 1) (2, 6) (3, 8) (4, 3) (5, 7) (6, 4) (7, 2) (8, 5) 

(1, 1) (2, 7) (3, 4) (4, 6) (5, 8) (6, 2) (7, 5) (8, 3) 

(1, 1) (2, 7) (3, 5) (4, 8) (5, 2) (6, 4) (7, 6) (8, 3) 

(1, 2) (2, 4) (3, 6) (4, 8) (5, 3) (6, 1) (7, 7) (8, 5) 

(1, 2) (2, 5) (3, 7) (4, 1) (5, 3) (6, 8) (7, 6) (8, 4) 

(1, 2) (2, 5) (3, 7) (4, 4) (5, 1) (6, 8) (7, 6) (8, 3) 

(1, 2) (2, 6) (3, 1) (4, 7) (5, 4) (6, 8) (7, 3) (8, 5) 

(1, 2) (2, 6) (3, 8) (4, 3) (5, 1) (6, 4) (7, 7) (8, 5) 

(1, 2) (2, 7) (3, 3) (4, 6) (5, 8) (6, 5) (7, 1) (8, 4) 

(1, 2) (2, 7) (3, 5) (4, 8) (5, 1) (6, 4) (7, 6) (8, 3) 

(1, 2) (2, 8) (3, 6) (4, 1) (5, 3) (6, 5) (7, 7) (8, 4) 

(1, 3) (2, 1) (3, 7) (4, 5) (5, 8) (6, 2) (7, 4) (8, 6) 

(1, 3) (2, 5) (3, 2) (4, 8) (5, 1) (6, 7) (7, 4) (8, 6) 

(1, 3) (2, 5) (3, 2) (4, 8) (5, 6) (6, 4) (7, 7) (8, 1) 

(1, 3) (2, 5) (3, 7) (4, 1) (5, 4) (6, 2) (7, 8) (8, 6) 

(1, 3) (2, 5) (3, 8) (4, 4) (5, 1) (6, 7) (7, 2) (8, 6) 

(1, 3) (2, 6) (3, 2) (4, 5) (5, 8) (6, 1) (7, 7) (8, 4) 

(1, 3) (2, 6) (3, 2) (4, 7) (5, 1) (6, 4) (7, 8) (8, 5) 

(1, 3) (2, 6) (3, 2) (4, 7) (5, 5) (6, 1) (7, 8) (8, 4) 

(1, 3) (2, 6) (3, 4) (4, 1) (5, 8) (6, 5) (7, 7) (8, 2) 

(1, 3) (2, 6) (3, 4) (4, 2) (5, 8) (6, 5) (7, 7) (8, 1) 

(1, 3) (2, 6) (3, 8) (4, 1) (5, 4) (6, 7) (7, 5) (8, 2) 

(1, 3) (2, 6) (3, 8) (4, 1) (5, 5) (6, 7) (7, 2) (8, 4) 

(1, 3) (2, 6) (3, 8) (4, 2) (5, 4) (6, 1) (7, 7) (8, 5) 

(1, 3) (2, 7) (3, 2) (4, 8) (5, 5) (6, 1) (7, 4) (8, 6) 

(1, 3) (2, 7) (3, 2) (4, 8) (5, 6) (6, 4) (7, 1) (8, 5) 

(1, 3) (2, 8) (3, 4) (4, 7) (5, 1) (6, 6) (7, 2) (8, 5) 

(1, 4) (2, 1) (3, 5) (4, 8) (5, 2) (6, 7) (7, 3) (8, 6) 

(1, 4) (2, 1) (3, 5) (4, 8) (5, 6) (6, 3) (7, 7) (8, 2) 

(1, 4) (2, 2) (3, 5) (4, 8) (5, 6) (6, 1) (7, 3) (8, 7) 

(1, 4) (2, 2) (3, 7) (4, 3) (5, 6) (6, 8) (7, 1) (8, 5) 

(1, 4) (2, 2) (3, 7) (4, 3) (5, 6) (6, 8) (7, 5) (8, 1) 

(1, 4) (2, 2) (3, 7) (4, 5) (5, 1) (6, 8) (7, 6) (8, 3) 

(1, 4) (2, 2) (3, 8) (4, 5) (5, 7) (6, 1) (7, 3) (8, 6) 

(1, 4) (2, 2) (3, 8) (4, 6) (5, 1) (6, 3) (7, 5) (8, 7) 

(1, 4) (2, 6) (3, 1) (4, 5) (5, 2) (6, 8) (7, 3) (8, 7) 

(1, 4) (2, 6) (3, 8) (4, 2) (5, 7) (6, 1) (7, 3) (8, 5) 

(1, 4) (2, 6) (3, 8) (4, 3) (5, 1) (6, 7) (7, 5) (8, 2) 

(1, 4) (2, 7) (3, 1) (4, 8) (5, 5) (6, 2) (7, 6) (8, 3) 

(1, 4) (2, 7) (3, 3) (4, 8) (5, 2) (6, 5) (7, 1) (8, 6) 

(1, 4) (2, 7) (3, 5) (4, 2) (5, 6) (6, 1) (7, 3) (8, 8) 

(1, 4) (2, 7) (3, 5) (4, 3) (5, 1) (6, 6) (7, 8) (8, 2) 

(1, 4) (2, 8) (3, 1) (4, 3) (5, 6) (6, 2) (7, 7) (8, 5) 

(1, 4) (2, 8) (3, 1) (4, 5) (5, 7) (6, 2) (7, 6) (8, 3) 

(1, 4) (2, 8) (3, 5) (4, 3) (5, 1) (6, 7) (7, 2) (8, 6) 

(1, 5) (2, 1) (3, 4) (4, 6) (5, 8) (6, 2) (7, 7) (8, 3) 

(1, 5) (2, 1) (3, 8) (4, 4) (5, 2) (6, 7) (7, 3) (8, 6) 

(1, 5) (2, 1) (3, 8) (4, 6) (5, 3) (6, 7) (7, 2) (8, 4) 

(1, 5) (2, 2) (3, 4) (4, 6) (5, 8) (6, 3) (7, 1) (8, 7) 

(1, 5) (2, 2) (3, 4) (4, 7) (5, 3) (6, 8) (7, 6) (8, 1) 

(1, 5) (2, 2) (3, 6) (4, 1) (5, 7) (6, 4) (7, 8) (8, 3) 

(1, 5) (2, 2) (3, 8) (4, 1) (5, 4) (6, 7) (7, 3) (8, 6) 

(1, 5) (2, 3) (3, 1) (4, 6) (5, 8) (6, 2) (7, 4) (8, 7) 

(1, 5) (2, 3) (3, 1) (4, 7) (5, 2) (6, 8) (7, 6) (8, 4) 

(1, 5) (2, 3) (3, 8) (4, 4) (5, 7) (6, 1) (7, 6) (8, 2) 

(1, 5) (2, 7) (3, 1) (4, 3) (5, 8) (6, 6) (7, 4) (8, 2) 

(1, 5) (2, 7) (3, 1) (4, 4) (5, 2) (6, 8) (7, 6) (8, 3) 

(1, 5) (2, 7) (3, 2) (4, 4) (5, 8) (6, 1) (7, 3) (8, 6) 

(1, 5) (2, 7) (3, 2) (4, 6) (5, 3) (6, 1) (7, 4) (8, 8) 

(1, 5) (2, 7) (3, 2) (4, 6) (5, 3) (6, 1) (7, 8) (8, 4) 

(1, 5) (2, 7) (3, 4) (4, 1) (5, 3) (6, 8) (7, 6) (8, 2) 

(1, 5) (2, 8) (3, 4) (4, 1) (5, 3) (6, 6) (7, 2) (8, 7) 

(1, 5) (2, 8) (3, 4) (4, 1) (5, 7) (6, 2) (7, 6) (8, 3) 

(1, 6) (2, 1) (3, 5) (4, 2) (5, 8) (6, 3) (7, 7) (8, 4) 

(1, 6) (2, 2) (3, 7) (4, 1) (5, 3) (6, 5) (7, 8) (8, 4) 

(1, 6) (2, 2) (3, 7) (4, 1) (5, 4) (6, 8) (7, 5) (8, 3) 

(1, 6) (2, 3) (3, 1) (4, 7) (5, 5) (6, 8) (7, 2) (8, 4) 

(1, 6) (2, 3) (3, 1) (4, 8) (5, 4) (6, 2) (7, 7) (8, 5) 

(1, 6) (2, 3) (3, 1) (4, 8) (5, 5) (6, 2) (7, 4) (8, 7) 

(1, 6) (2, 3) (3, 5) (4, 7) (5, 1) (6, 4) (7, 2) (8, 8) 

(1, 6) (2, 3) (3, 5) (4, 8) (5, 1) (6, 4) (7, 2) (8, 7) 

(1, 6) (2, 3) (3, 7) (4, 2) (5, 4) (6, 8) (7, 1) (8, 5) 

(1, 6) (2, 3) (3, 7) (4, 2) (5, 8) (6, 5) (7, 1) (8, 4) 

(1, 6) (2, 3) (3, 7) (4, 4) (5, 1) (6, 8) (7, 2) (8, 5) 

(1, 6) (2, 4) (3, 1) (4, 5) (5, 8) (6, 2) (7, 7) (8, 3) 

(1, 6) (2, 4) (3, 2) (4, 8) (5, 5) (6, 7) (7, 1) (8, 3) 

(1, 6) (2, 4) (3, 7) (4, 1) (5, 3) (6, 5) (7, 2) (8, 8) 

(1, 6) (2, 4) (3, 7) (4, 1) (5, 8) (6, 2) (7, 5) (8, 3) 

(1, 6) (2, 8) (3, 2) (4, 4) (5, 1) (6, 7) (7, 5) (8, 3) 

(1, 7) (2, 1) (3, 3) (4, 8) (5, 6) (6, 4) (7, 2) (8, 5) 

(1, 7) (2, 2) (3, 4) (4, 1) (5, 8) (6, 5) (7, 3) (8, 6) 

(1, 7) (2, 2) (3, 6) (4, 3) (5, 1) (6, 4) (7, 8) (8, 5) 

(1, 7) (2, 3) (3, 1) (4, 6) (5, 8) (6, 5) (7, 2) (8, 4) 

(1, 7) (2, 3) (3, 8) (4, 2) (5, 5) (6, 1) (7, 6) (8, 4) 

(1, 7) (2, 4) (3, 2) (4, 5) (5, 8) (6, 1) (7, 3) (8, 6) 

(1, 7) (2, 4) (3, 2) (4, 8) (5, 6) (6, 1) (7, 3) (8, 5) 

(1, 7) (2, 5) (3, 3) (4, 1) (5, 6) (6, 8) (7, 2) (8, 4) 

(1, 8) (2, 2) (3, 4) (4, 1) (5, 7) (6, 5) (7, 3) (8, 6) 

(1, 8) (2, 2) (3, 5) (4, 3) (5, 1) (6, 7) (7, 4) (8, 6) 

(1, 8) (2, 3) (3, 1) (4, 6) (5, 2) (6, 5) (7, 7) (8, 4) 

(1, 8) (2, 4) (3, 1) (4, 3) (5, 6) (6, 2) (7, 7) (8, 5) 

共耗时:
0.05162310600280762 s

一共有92种解法

你可能感兴趣的:(算法)