N-queens

From emboxit
Revision as of 20:51, 11 September 2015 by Admin (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Tested code with n=4 <python> BOARD_SIZE = 4

def under_attack(col, queens):

   return col in queens or \
          any(abs(col - x) == len(queens)-i for i,x in enumerate(queens))

def solve(n):

   solutions = [[]]
   for row in range(n):
       solutions = (solution+[i+1]
                      for solution in solutions # first for clause is evaluated immediately,
                                                # so "solutions" is correctly captured
                      for i in range(BOARD_SIZE)
                      if not under_attack(i+1, solution))
   return solutions

answers = solve(BOARD_SIZE) first_answer = next(answers) print(list(enumerate(first_answer, start=1)))

</python>