class Solution: # @return a list of lists of string def under_attack(self,col, queens): return col in queens or any(abs(col - x) == len(queens)-i for i,x in enumerate(queens)) def solve(self,n): solutions = [[]] for row in range(n): solutions = (solution+[i] for solution in solutions # first for clause is evaluated immediately, # so "solutions" is correctly captured for i in range(n) if not self.under_attack(i, solution)) return solutions def solveNQueens(self, n): slt=[] for j in self.solve(n): if len(j)==n: slt.append(list('.'*i+'Q'+'.'*(n-1-i) for i in j)) return slt
Friday, September 12, 2014
Leetcode: N-Queens @Python
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment