-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathNQueensIi.java
More file actions
118 lines (103 loc) · 3.54 KB
/
Copy pathNQueensIi.java
File metadata and controls
118 lines (103 loc) · 3.54 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
* <p><strong>n 皇后问题</strong> 研究的是如何将 <code>n</code> 个皇后放置在 <code>n × n</code> 的棋盘上,并且使皇后彼此之间不能相互攻击。</p>
*
* <p>给你一个整数 <code>n</code> ,返回 <strong>n 皇后问题</strong> 不同的解决方案的数量。</p>
*
* <p> </p>
*
* <div class="original__bRMd">
* <div>
* <p><strong>示例 1:</strong></p>
* <img alt="" src="https://assets.leetcode.com/uploads/2020/11/13/queens.jpg" style="width: 600px; height: 268px;" />
* <pre>
* <strong>输入:</strong>n = 4
* <strong>输出:</strong>2
* <strong>解释:</strong>如上图所示,4 皇后问题存在两个不同的解法。
* </pre>
*
* <p><strong>示例 2:</strong></p>
*
* <pre>
* <strong>输入:</strong>n = 1
* <strong>输出:</strong>1
* </pre>
*
* <p> </p>
*
* <p><strong>提示:</strong></p>
*
* <ul>
* <li><code>1 <= n <= 9</code></li>
* </ul>
* </div>
* </div>
* <div><div>Related Topics</div><div><li>回溯</li></div></div><br><div><li>👍 325</li><li>👎 0</li></div>
*/
package leetcode8;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.concurrent.atomic.LongAdder;
public class NQueensIi {
public static void main(String[] args) {
new NQueensIi().new Solution().totalNQueens(4);
}
/**
* 该题正解,位运算
* 因为已限定 n < 32,int类型可以作为记忆数组使用
*/
class Solution {
public int totalNQueens(int n) {
return solve(n, 0, 0, 0, 0);
}
private int solve(int n, int column, int left, int right, int level) {
if (level >= n) return 1;
// 当前棋盘未填充的位置 = (32位棋盘未填充的位置) & (当前棋盘的大小)
int bits = (~(column | left | right)) & ((1 << n) - 1);
int res = 0;
// 未填充的位置进行遍历
while (bits != 0) {
// 选取末位的1 : x & -x
int pick = bits & -bits;
// 下探到下一层
res += solve(n, column | pick, (left | pick) << 1, (right | pick) >>> 1, level + 1);
// 清零末位的1 : x & ( x-1 )
bits = bits & (bits - 1);
}
return res;
}
}
/**
* 使用int实例变量代替 {@link LongAdder},性能明显高了很多
*/
class Solution2 {
int adder = 0;
public int totalNQueens(int n) {
Deque<Integer> deque = new ArrayDeque<>(n);
boolean[] left = new boolean[2 * n - 1];
boolean[] right = new boolean[2 * n - 1];
boolean[] column = new boolean[n];
resolve(deque, n, left, right, column, 0);
return adder;
}
private void resolve(Deque<Integer> deque, int n, boolean[] left, boolean[] right, boolean[] column, int y) {
if (y >= n) {
adder++;
return;
}
for (int x = 0; x < n; x++) {
if (left[x + y] || right[x - y + n - 1] || column[x]) {
continue;
}
deque.addLast(x);
left[x + y] = true;
right[x - y + n - 1] = true;
column[x] = true;
resolve(deque, n, left, right, column, y + 1);
deque.removeLast();
left[x + y] = false;
right[x - y + n - 1] = false;
column[x] = false;
}
}
}
}