daily pastebin goal
45%
SHARE
TWEET

Untitled

a guest Sep 24th, 2018 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. BOARD_SIZE = 8
  2.  
  3. def under_attack(col, queens):
  4.   left = right = col
  5.  
  6.   for r, c in reversed(queens):
  7.     left, right = left - 1, right + 1
  8.  
  9.     if c in (left, col, right):
  10.       return True
  11.   return False
  12.  
  13. def solve(n):
  14.   if n == 0:
  15.     return [[]]
  16.  
  17.   smaller_solutions = solve(n - 1)
  18.  
  19.   return [solution+[(n,i+1)]
  20.     for i in xrange(BOARD_SIZE)
  21.       for solution in smaller_solutions
  22.         if not under_attack(i+1, solution)]
  23.        
  24. for answer in solve(BOARD_SIZE):
  25.   print answer
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top