52.N皇后Ⅱ
2018-05-11 本文已影响0人
_道友请留步_
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);
}
}
}
}