class Solution {
public int totalNQueens(int n) {
int[] nums = new int[n];
for(int i = 0; i < n; i++){
nums[i] = Integer.MIN_VALUE;
}
int[] count = new int[1];
queen(count, nums, 0);
return count[0];
}
public void queen(int[] count, int[] nums, int cur){
if(cur == nums.length){
count[0]++;
return;
}
for(int col = 0; col < nums.length; col++){
nums[cur] = col;
boolean flag = true;
for(int row = 0; row < cur; row++){
if(nums[row] == col || Math.abs(col - nums[row]) == cur - row){
flag =false;
break;
}
}
if(flag){
queen(count, nums, cur+1);
}
}
}
}
网友评论