N 皇后

位运算解答

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
var size = 0
var count = 0
func totalNQueens(_ n: Int) -> Int {
size = (1 << n) - 1
dfs(0,0,0)
return count
}
func dfs(_ row: Int, _ ld: Int, _ rd: Int) {
if row == size {
count += 1
return
}
var pos = size & (~(row|ld|rd))
while pos != 0 {
var p = pos&(-pos)
pos = pos&(pos-1)
dfs(row|p, (ld|p)<<1, (rd|p)>>1)
}
}
}

回溯

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
class Solution {
func solveNQueens(_ n: Int) -> [[String]] {
guard n > 0 else {
return []
}
var results = [[String]]()
// 存储每个皇后的 x 坐标
var cols = [Int]()
dfsHelper(n, &cols, &results)
return results
}
fileprivate func dfsHelper(_ n: Int, _ cols: inout [Int], _ results: inout [[String]]) {
if cols.count == n {
results.append(draw(cols))
return
}
for i in 0..<n {
guard isValid(cols, i) else {
continue
}
cols.append(i)
dfsHelper(n, &cols, &results)
cols.removeLast()
}
}
fileprivate func isValid(_ cols: [Int], _ colIndex: Int) -> Bool {
for rowIndex in 0..<cols.count {
// 竖向
if colIndex == cols[rowIndex] {
return false
}
// 左对角线
if cols[rowIndex] - rowIndex == colIndex - cols.count {
return false
}
// 右对角线
if cols[rowIndex] + rowIndex == colIndex + cols.count {
return false
}
}
return true
}
fileprivate func draw(_ cols: [Int]) -> [String] {
var result = [String]()
for rowIndex in 0..<cols.count {
var row = ""
for j in 0..<cols.count {
row += cols[rowIndex] == j ? "Q" : "."
}
result.append(row)
}
return result
}
}


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!