cols=[-1,-1,-1,-1, -1,-1,-1,-1]
depth = 0 #成功的行数
count = 0
def isValid(row , col):
for i in range(0, row):
if cols[i] == col:
return False
if abs(cols[i] - col) == row - i :
return False
return True
def placeRow(row):
global depth,count
for col in range(0, len(cols)):
if isValid(row, col):
cols[row] = col
depth += 1
if len(cols) == depth:
print("succ:", cols)
count += 1
placeRow(row+1)
depth -= 1
if __name__ == "__main__":
placeRow(0)
print("matched count:", count)
8皇后的答案:92
4皇后的答案:2