FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Leetcode-Py/N-Queens.py at master · SocialfiPanda/Leetcode-Py · GitHub
SocialfiPanda
/
Leetcode-Py
Public
Notifications
You must be signed in to change notification settings
Fork
128
Star
280
Code
Issues
1
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
Leetcode-Py
/
N-Queens.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
27 lines (23 loc) · 1.23 KB
Breadcrumbs
Leetcode-Py
/
N-Queens.py
Copy path
File metadata and controls
27 lines (23 loc) · 1.23 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
class
Solution
:
def
solveNQueens
(
self
,
n
):
solutions
=
[]
self
.
solveRecur
(
solutions
, [],
n
,
0
)
return
solutions
def
solveRecur
(
self
,
solutions
,
solution
,
n
,
row
):
if
row
==
n
:
solutions
.
append
(
map
(
lambda
x
:
'.'
*
x
+
"Q"
+
'.'
*
(
n
-
x
-
1
),
solution
))
for
i
in
range
(
n
):
if
i
not
in
solution
and
reduce
(
lambda
acc
,
j
:
abs
(
row
-
j
)
!=
abs
(
i
-
solution
[
j
])
and
acc
,
range
(
len
(
solution
)),
True
):
self
.
solveRecur
(
solutions
,
solution
+
[
i
],
n
,
row
+
1
)
# The alternative is to make one line reduce function a separate method. Either way is fine.
# def is_diagonally_legal(self, solution, n, row, col):
# for i in range(len(solution)):
# if abs(row - i) == abs(col - solution[i]):
# return False
# return True
# def solveNQueensRecur(self, solutions, solution, n, i):
# if i == n:
# solutions.append(map(lambda x: '.' * x + "Q" + '.' * (n - x - 1), solution))
# for j in range(n):
# if j not in solution and self.is_diagonally_legal(solution, n, i, j):
# self.solveNQueensRecur(solutions, solution + [j], n, i + 1)
Back
|
FazBrowse Home
|
New Git URL