递归解决:
n = 8
x = [0 for i in range(n)]
sum_ans = 0
def place(k):
for i in range(k):
if abs(x[i]-x[k]) == abs(i-k) or x[i] == x[k]:
return False
return True
def backtrak(t):
if t == n:
global sum_ans
sum_ans += 1
print x
else:
for i in range(n):
x[t] = i
if place(t):
backtrak(t+1)
backtrak(0)
print sum_ans