Backtracking example ―n Queen algorithm (Java implementation)

Source: Internet
Author: User

Problem Description: Place a n*n on the chessboard of the other N-Queens that are not subject to attack. According to the rules of chess, the Queen can attack pieces that are on the same line or in the same column or on the same slash. The question after n is equivalent to placing n Queens on the Board of the N*n, and any 2 queens are not placed on the same row or column or on the same slash.


Math.Abs (k-j) ==math.abs (x[j]-x[k]) | | (X[j]==x[k])//satisfies this article, stating that the condition is not met, that is, in the same row or the same column or the same slash

Import Java.util.scanner;public class BackTrack {public static void main (String [] agrs) {Scanner sc = new Scanner (system.i n); System.out.println ("Please enter N:"); int n = sc.nextint (); System.out.println (n+ "Queen's Solution has" +track (n) + "species");} public static long track (int n) {Queen X = new Queen ();//Use recursive method Queen Y = new Queen ();//Use iterative method X.N = n; x.sum = 0; Y.N = n; Y.sum = 0;int[] arr = new int[n+1];for (int i=0;i<=n;i++) {arr[i]=0;} x.x = arr; y.x = Arr;long StartTime = System.currenttimemillis (); X.backtrack (1); Long endTime = System.currenttimemillis (); System.out.println ("Run time using recursive method:" + (Endtime-starttime) + "MS"); for (int i=0;i<=n;i++) {y.x[i]=0;} StartTime = System.currenttimemillis (); Y.whilebacktrack (); endTime = System.currenttimemillis (); System.out.println ("Run Time with loop method:" + (Endtime-starttime) + "MS"); return x.sum;}} class Queen{public int n;//Queen number public int[] x;//Current solution index represents row X[index] represents the column public long sum;//the number of viable scenarios that are currently found private Boolean Place (int k) {//Check the feasibility of line K for (int j=1;j<k;j++) {if (Math.Abs (k-j) ==math.abs (X[j]-x[k])|| (X[j]==x[k])) Return false;//description does not meet the criteria}return true;//description conforms to the condition}public void backTrack (int t) {//Recursive method if (t>n) {///when it arrives at the leaf node, That is, all the layers are searched sum++;//feasible number of +1printway ();//Print out the current solution}else{for (int i=1;i<=n;i++) {//Each loop Right one column x[t] = i;if (place (t)) BackTrack (t+1);//depth precedence recursively searches}}}public long whilebacktrack () {//loop method x[1] = 0;int K=1;while (k>0) {x[k] + = 1;while ((x[ K]<=n) &&! (Place (k))) x[k]+=1;//is not eligible to move right one column until eligible or out-of-bounds if (x[k]<=n) {//Exits while loop without crossing if (K==n) sum++;//arrives at the last row, the number of feasible scenarios +1else{k++;//Move down a row x[ K] = 0;//assigns the initial value to the 0}}else k--;//indicates that this method does not work, to return a row}return sum;} public void Printway () {for (int i=1;i<=n;i++) {System.out.print ("(" +i+ "," +x[i]+ ")");} System.out.println ();}}
Run:



Backtracking example ―n Queen algorithm (Java implementation)

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.