pos=[0]*5
x=[0]*5
def place(t):
for i in range(1,t):
if (pos[i] == pos[t]):
return False
elif (abs(pos[i]-pos[t]) == abs(i-t)):
return False
return True
def Backtrack(t,n):
if (t>n):
print "get an answer"
print pos
else:
for i in range(1, n+1):
pos[t]=i
if (place(t)):
Backtrack(t+1, n)
def Queen(n):
Backtrack(1,n)
Queen(4)
时间: 2024-12-28 11:55:26