-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy path51_NQueens.py
executable file
·58 lines (49 loc) · 2.04 KB
/
51_NQueens.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
53
54
55
56
57
58
#! /usr/bin/env python
# -*- coding: utf-8 -*-
class Solution(object):
allNQueens = []
def solveNQueens(self, n):
self.allNQueens = []
self.cols = [True] * n
self.left_right = [True] * (2 * n - 1)
self.right_left = [True] * (2 * n - 1)
queueMatrix = [["."] * n for row in range(n)]
self.solve(0, queueMatrix, n)
return self.allNQueens
def solve(self, row, matrix, n):
"""
Refer to:
https://discuss.leetcode.com/topic/13617/accepted-4ms-c-solution-use-backtracking-and-bitmask-easy-understand
The number of columns is n, the number of 45° diagonals is 2 * n - 1,
the number of 135° diagonals is also 2 * n - 1.
When reach [row, col], the column No. is col,
the 45° diagonal No. is row + col and the 135° diagonal No. is n - 1 + col - row.
| | | / / / \ \ \
O O O O O O O O O
| | | / / / / \ \ \ \
O O O O O O O O O
| | | / / / / \ \ \ \
O O O O O O O O O
| | | / / / \ \ \
3 columns 5 45° diagonals 5 135° diagonals (when n is 3)
"""
# Get one Queen Square
if row == n:
result = ["".join(r) for r in matrix]
self.allNQueens.append(result)
return
for col in range(n):
if self.cols[col] and self.left_right[row + n - 1 - col] and self.right_left[row + col]:
matrix[row][col] = "Q"
self.cols[col] = self.left_right[row + n - 1 - col] = self.right_left[row + col] = False
# Solve the child question
self.solve(row + 1, matrix, n)
# Backtracking here.
matrix[row][col] = "."
self.cols[col] = self.left_right[
row + n - 1 - col] = self.right_left[row + col] = True
"""
1
5
8
"""