N Queen question public class nqueue{//Violent recursive method public static int num01 (int n) {if (n<1) {return 0;
} int[]record=new Int[n];
Return process01 (0,record,n);
public static int process01 (int i,int[]record,int n) {if (i==n) {return 1;
int res=0;
for (int j=0;j<n;j++) {if (IsValid (record,i,j)) {record[i]=j;
Res+=process01 (I+1,record,n);
} return res; public static Boolean IsValid (Int[]record,int I,int j) {for (int k=0;k<i;k++) {if (j==record[k]| |
Math.Abs (record[k]-j) ==math.abs (i-k)) {return false;
} return true;
//************************************************************************//optimized method public static int num02 (int n) {
Because the vector of the median operation of this method is an int variable, the method can only be counted as the 1~32 Queen problem//If you want to compute more queens, use a variable containing more bits if (N < 1 | | n >) {return 0; int Upperlim = n = = 32?
-1: (1 << N)-1;
Return Process2 (upperlim, 0, 0, 0); public static int Process2 (int upperlim, int collim,T Leftdialim, int rightdialim) {if (Collim = = Upperlim) {return 1;
int pos = 0;
int mostrightone = 0;
pos = Upperlim & (~ (Collim | leftdialim | rightdialim));
int res = 0;
while (POS!= 0) {Mostrightone = pos & (~pos + 1);
pos = Pos-mostrightone; Res + + process2 (Upperlim, Collim | mostrightone, Leftdialim | mostrightone) << 1, (Rightdialim | mostrigh
TOne) >>> 1);
return res;
public static void Main (String[]args) {int n=8;
System.out.println (NUM01 (n));
System.out.println (NUM02 (n)); }
}