52:N皇后 II
This commit is contained in:
parent
d443e0e0c7
commit
8a2a98e5bd
87
src/main/java/leetcode/editor/cn/NQueensIi.java
Normal file
87
src/main/java/leetcode/editor/cn/NQueensIi.java
Normal file
@ -0,0 +1,87 @@
|
||||
//n 皇后问题 研究的是如何将 n 个皇后放置在 n × n 的棋盘上,并且使皇后彼此之间不能相互攻击。
|
||||
//
|
||||
// 给你一个整数 n ,返回 n 皇后问题 不同的解决方案的数量。
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:n = 4
|
||||
//输出:2
|
||||
//解释:如上图所示,4 皇后问题存在两个不同的解法。
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:n = 1
|
||||
//输出:1
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 1 <= n <= 9
|
||||
//
|
||||
//
|
||||
//
|
||||
// Related Topics 回溯 👍 340 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
//52:N皇后 II
|
||||
public class NQueensIi {
|
||||
public static void main(String[] args) {
|
||||
Solution solution = new NQueensIi().new Solution();
|
||||
|
||||
}
|
||||
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int totalNQueens(int n) {
|
||||
Set<Integer> columns = new HashSet<Integer>();
|
||||
Set<Integer> diagonals1 = new HashSet<Integer>();
|
||||
Set<Integer> diagonals2 = new HashSet<Integer>();
|
||||
return backtrack(n, 0, columns, diagonals1, diagonals2);
|
||||
}
|
||||
|
||||
public int backtrack(int n, int row, Set<Integer> columns, Set<Integer> diagonals1, Set<Integer> diagonals2) {
|
||||
if (row == n) {
|
||||
return 1;
|
||||
} else {
|
||||
int count = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (columns.contains(i)) {
|
||||
continue;
|
||||
}
|
||||
int diagonal1 = row - i;
|
||||
if (diagonals1.contains(diagonal1)) {
|
||||
continue;
|
||||
}
|
||||
int diagonal2 = row + i;
|
||||
if (diagonals2.contains(diagonal2)) {
|
||||
continue;
|
||||
}
|
||||
columns.add(i);
|
||||
diagonals1.add(diagonal1);
|
||||
diagonals2.add(diagonal2);
|
||||
count += backtrack(n, row + 1, columns, diagonals1, diagonals2);
|
||||
columns.remove(i);
|
||||
diagonals1.remove(diagonal1);
|
||||
diagonals2.remove(diagonal2);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
33
src/main/java/leetcode/editor/cn/doc/content/NQueensIi.md
Normal file
33
src/main/java/leetcode/editor/cn/doc/content/NQueensIi.md
Normal file
@ -0,0 +1,33 @@
|
||||
<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>👍 340</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user