#-*-coding:utf-8-*- count = 0 locs = [] def check_p(i,j): for x in range(len(locs)): if locs[x] == i: return False if abs(locs[x] - i) == abs(x - j): return False return True def main(j): global count,locs for i in range(8): if check_p(i,j) == True: locs.append(i) if j == 7: count += 1 print locs locs.pop() return main(j + 1) locs.pop() if __name__ == "__main__": main(0) print count
主要思路就是递归和回溯