-
Notifications
You must be signed in to change notification settings - Fork 0
/
51-N-Queens.py
52 lines (36 loc) · 1.21 KB
/
51-N-Queens.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'''Leetcode- https://leetcode.com/problems/n-queens/ '''
'''
The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other.
Given an integer n, return all distinct solutions to the n-queens puzzle. You may return the answer in any order.
Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space, respectively.
Example 1:
Input: n = 4
Output: [[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]
'''
def solveNQueens(n):
cols = set()
pos = set()
neg = set()
res = []
board = [["."] * n for i in range(n)]
def backtrack(r):
if r == n:
copy = ["".join(row) for row in board]
res.append(copy)
return
for c in range(n):
if c in cols or (r + c) in pos or (r - c) in neg:
continue
cols.add(c)
pos.add(r+c)
neg.add(r-c)
board[r][c] = "Q"
backtrack(r+1)
cols.remove(c)
pos.remove(r+c)
neg.remove(r-c)
board[r][c] = "."
backtrack(0)
return res
#T:O(n!)
#S:O(N^2)