[leetcode] 52. N皇后 II
Posted ACBingo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[leetcode] 52. N皇后 II相关的知识,希望对你有一定的参考价值。
跟上个题一模一样,现在只需输出个数即可
class Solution {
public int totalNQueens(int n) {
boolean[] row = new boolean[n];
boolean[] h = new boolean[2 * n];
boolean[] r = new boolean[2 * n];
List<List<String>> ans = new ArrayList<>();
dfs(n, row, h, r, new ArrayList<>(), 0, ans);
return ans.size();
}
public void dfs(int n, boolean[] row, boolean[] h, boolean[] r, List<Integer> curList, int curK, List<List<String>> ans) {
if (curK == n) {
List<String> tmp = new ArrayList<>();
for (Integer integer : curList) {
String st = "";
for (int i = 0; i < n; i++) {
if (i == integer) {
st += "Q";
} else {
st += ".";
}
}
tmp.add(st);
}
ans.add(tmp);
}
for (Integer i = 0; i < n; i++) {
if (!row[i] && !h[curK + i] && !r[curK - i + n]) {
row[i] = true;
h[curK + i] = true;
r[curK - i + n] = true;
curList.add(i);
dfs(n, row, h, r, curList, curK + 1, ans);
curList.remove(i);
row[i] = false;
h[curK + i] = false;
r[curK - i + n] = false;
}
}
}
}
以上是关于[leetcode] 52. N皇后 II的主要内容,如果未能解决你的问题,请参考以下文章