选书 回溯 python

data = [
    [0,0,0,0,0,0],
    [0,0,0,1,1,0],
    [0,1,1,0,0,1],
    [0,0,1,1,0,0],
    [0,0,0,0,1,0],
    [0,0,1,0,0,1]
    ]

book = [0 for i in range(6)]
flag = [1 for i in range(6)]


def dfs(i):
    for j in range(1,6):
        if flag[j] == 1 and data[i][j] == 1:
            flag[j] = 0
            book[i] = j
            if i == 5:
                for k in range(1,6):
                    print(chr(64+book[k]),end=" ")
                print()
            else:
                dfs(i+1)
            flag[j] = 1
            book[i] = 0


        


dfs(1)

你可能感兴趣的:(Python,竞赛)