Skip to content

Commit

Permalink
完成第10章N皇后问题。
Browse files Browse the repository at this point in the history
  • Loading branch information
zwdnet committed May 29, 2020
1 parent 1ba0107 commit e43db9a
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 9 deletions.
35 changes: 26 additions & 9 deletions 44/09/talent.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@
# 9.脱口秀节目


Talent = ["Sing", "Dance", "Magic", "Act", "Flex", "Code"]
Talents = ["Sing", "Dance", "Magic", "Act", "Flex", "Code"]
Candidates = ["Aly", "Bob", "Cal", "Don", "Eve", "Fay"]
CandidateTalents = [["Flex", "Code"],
["Dance", "Magic"],
["Sing", "Magic"],
["Dance", "Act", "Code"],
["Act", "Code"]]
CandidateTalents = [ ['Flex', 'Code'], ['Dance', 'Magic'], ['Sing', 'Magic'], ['Sing', 'Dance'], ['Dance', 'Act', 'Code'], ['Act', 'Code'] ]


# 生成所有候选人组合
Expand All @@ -19,13 +15,34 @@ def Hire4Show(candList, candTalents, talentList):
for i in range(2**n):
Combination = []
num = i
for j in range(n):
for j in range(n):
if (num % 2 == 1):
Combination = [candList[n-1-j]] + Combination
num = nun//2
num = num // 2

if Good(Combination, candList, candTalents, talentList):
if len(hire) > len(Combination):
hire = Combination

print ('Optimum Solution:', hire)


def Good(Comb, candList, candTalents, AllTalents):
for tal in AllTalents:
cover = False
for cand in Comb:
candTal = candTalents[candList.index(cand)]
if tal in candTal:
cover = True
if not cover:
return False
return True


if __name__ == "__main__":
pass
Hire4Show(Candidates, CandidateTalents, Talents)

ST2 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
CL2 = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
CT2 = [ [4, 5, 7], [1, 2, 8], [2, 4, 6, 9], [3, 6, 9], [2, 3, 9], [7, 8, 9], [1, 3, 7] ]
Hire4Show(CL2, CT2, ST2)
95 changes: 95 additions & 0 deletions 44/10/NQueen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# coding:utf-8
# 《programming for the puzzled》实操
# 10.更多的皇后


# 迭代 欧几里得算法求最大公约数
def iGcd(m, n):
while n > 0:
m, n = n, m%n
return m


# 递归版本求最大公约数
def rGcd(m, n):
if m%n == 0:
return n
else:
gcd = rGcd(n, m%n)
return gcd


# 斐波那契数列
def rFib(x):
if x == 0:
return 0
elif x == 1:
return 1
else:
return rFib(x-1) + rFib(x-2)


# 迭代法计算斐波那契数列
def iFib(x):
if x < 2:
return x
else:
f, g = 0, 1
for i in range(x-1):
f, g = g, f+g
return g


# 递归法解n皇后问题
def nQueens(size):
board = [-1] * size
rQueens(board, 0, size)
print (board)
displayBoard(board)


def noConflicts(board, current):
for i in range(current):
if (board[i] == board[current]):
return False
if (current - i == abs(board[current] - board[i])):
return False
return True


def rQueens(board, current, size):
if (current == size):
return True
else:
for i in range(size):
board[current] = i
if (noConflicts(board, current)):
done = rQueens(board, current + 1, size)
if (done):
return True
return False


# 画棋盘
def displayBoard(board):
n = len(board)
for i in range(n):
for j in range(board[i]):
print(".", end='')
print("Q", end='')
for j in range(board[i]+1, n):
print(".", end='')
print("\n")



if __name__ == "__main__":
print(iGcd(24, 100))
print(rGcd(24, 100))
n = int(input("输入n值:"))
for i in range(n):
print(rFib(i))
for i in range(n):
print(iFib(i))
nQueens(n)

0 comments on commit e43db9a

Please sign in to comment.