Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. # Look ma, no modules!
  2.  
  3. # bottlenecks around 12, when the problem size starts to mushroom
  4. board_size = 11
  5.  
  6. solutions = []
  7. queens = []
  8. occupied = board_size*[0,]
  9.  
  10.  
  11.  
  12. def explore(depth):
  13. # base case
  14. if(depth==board_size):
  15. # stringify/serialize the solution
  16. solutions.append("%s"%(queens))
  17. return
  18.  
  19. else:
  20. attacked = 2*board_size*[0,]
  21. for i in range(0,len(queens)):
  22. ix1 = queens[i] + depth - i
  23. attacked[ix1] = 1
  24.  
  25. ix2 = queens[i] - depth + i
  26. attacked[ix2] = 1
  27.  
  28. for row in range(0,board_size):
  29. if(occupied[row] or attacked[row]):
  30. continue
  31.  
  32. # make a choice
  33. queens.append(row)
  34. occupied[row] = 1
  35.  
  36. # explore the consequences
  37. explore(depth+1)
  38.  
  39. # unmake the choice
  40. queens.pop()
  41. occupied[row] = 0
  42.  
  43.  
  44.  
  45. explore(0)
  46. print "Found %d solutions"%(len(solutions))
  47. #print solutions
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement