N Queen question __n queen question

Source: Internet
Author: User
Tags abs

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)); }
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.