fork download
  1. queen = [0 for c in range(8)]
  2. rfree = [True for r in range(8)]
  3. du = [True for i in range(15)]
  4. dd = [True for i in range(15)]
  5.  
  6. def solve(c):
  7. global solutions
  8.  
  9. if c == 8:
  10. solutions += 1
  11. print(solutions, end = ": ")
  12. for r in range(8):
  13. print (queen[r] + 1 ,end = " " if r<7 else "\n")
  14. else:
  15. for r in range(8):
  16. if rfree[r] and dd[c + r] and du[ c+7-r]:
  17. queen[c] = r
  18. rfree[r] = dd[ c+r ] = du[ c+7-r ] = False
  19. solve(c+1)
  20. rfree[r] = dd[ c+r ] = du[ c+7-r ] = True
  21.  
  22. solutions = 0
  23. solve(0)
  24. print()
  25. print(solutions, "solutions")
Success #stdin #stdout 0.02s 27712KB
stdin
Standard input is empty
stdout
1: 1 5 8 6 3 7 2 4
2: 1 6 8 3 7 4 2 5
3: 1 7 4 6 8 2 5 3
4: 1 7 5 8 2 4 6 3
5: 2 4 6 8 3 1 7 5
6: 2 5 7 1 3 8 6 4
7: 2 5 7 4 1 8 6 3
8: 2 6 1 7 4 8 3 5
9: 2 6 8 3 1 4 7 5
10: 2 7 3 6 8 5 1 4
11: 2 7 5 8 1 4 6 3
12: 2 8 6 1 3 5 7 4
13: 3 1 7 5 8 2 4 6
14: 3 5 2 8 1 7 4 6
15: 3 5 2 8 6 4 7 1
16: 3 5 7 1 4 2 8 6
17: 3 5 8 4 1 7 2 6
18: 3 6 2 5 8 1 7 4
19: 3 6 2 7 1 4 8 5
20: 3 6 2 7 5 1 8 4
21: 3 6 4 1 8 5 7 2
22: 3 6 4 2 8 5 7 1
23: 3 6 8 1 4 7 5 2
24: 3 6 8 1 5 7 2 4
25: 3 6 8 2 4 1 7 5
26: 3 7 2 8 5 1 4 6
27: 3 7 2 8 6 4 1 5
28: 3 8 4 7 1 6 2 5
29: 4 1 5 8 2 7 3 6
30: 4 1 5 8 6 3 7 2
31: 4 2 5 8 6 1 3 7
32: 4 2 7 3 6 8 1 5
33: 4 2 7 3 6 8 5 1
34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