-
Notifications
You must be signed in to change notification settings - Fork 0
/
51.py
33 lines (26 loc) · 848 Bytes
/
51.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution(object):
def solveNQueens(self, n):
"""
:type n: int
:rtype: List[List[str]]
"""
ret = []
self.recursive([-1]*n, 0, [], ret)
return ret
def recursive(self, nums, index, path, ret):
if index==len(nums):
ret.append(path)
return
for i in xrange(len(nums)):
nums[index]=i
if self.isValid(nums, index):
temp = '.' * len(nums)
self.recursive(nums, index+1, path+[temp[:i]+'Q'+temp[i+1:]], ret)
def isValid(self, nums, index):
for i in xrange(index):
if abs(nums[i]-nums[index])==index-i or nums[i]==nums[index]:
return False
return True
if __name__ == '__main__':
solution = Solution()
print solution.solveNQueens(8)