//定义一个数组,每个数组的值代表皇后的位置,没放一步都判断当前位置是否有效,有效则继续下一步,知道n 步都正确,次数加1 ,遍历得到所有的次数
import java.util.*;
public class Solution {
/**
*
* @param n int整型 the n
* @return int整型
*/
public int Nqueen (int n) {
int[] queen = new int[n];
int res = 0;
res = getQueen(queen, 1, n, res);
return res;
// write code here
}
public int getQueen(int[] queen, int steep, int n, int res) {
for (int i = 1; i <= n; i++) {
queen[steep - 1] = i;
if (curSteep(queen, steep)) {
if (steep == n) {
res++;
} else {
res = getQueen(queen, steep + 1, n, res);
}
}
}
return res;
}
public boolean curSteep(int[] queen, int steep) {
for (int j = 0; j < steep - 1; j++) {
if (queen[j] == queen[steep - 1] ||
Math.abs(queen[steep - 1] - queen[j]) == steep - j - 1) {
return false;
}
}
return true;
}
}