N皇后
dfs模板
点击查看代码
class Solution {
private int ans;
public int totalNQueens(int n) {
boolean[] col = new boolean[n];
boolean[] diag1 = new boolean[n*2 - 1];
boolean[] diag2 = new boolean[n*2 - 1];
dfs(0, col, diag1, diag2);
return ans;
}
private void dfs(int r, boolean[] col, boolean[] diag1, boolean[] diag2){
int n = col.length;
if(r == n){
ans++;
return;
}
for(int c = 0; c < n; c++){
int rc = r-c+n-1;//计算当前行r和列c对应的右斜线diag2的索引。
if(!col[c] && !diag1[r+c]&&!diag2[rc]){
col[c] = diag1[r+c] = diag2[rc] =true;
dfs(r+1,col,diag1,diag2);
col[c] = diag1[r+c] =diag2[rc]=false;
}
}
}
}