Backtracking algorithm solving eight Queens Problem (Java edition)

Source: Internet
Author: User

The eight Queens problem is a problem that has to be mentioned when learning backtracking algorithm, and it is simpler to solve the problem by backtracking algorithm.

The following is a Java version of the backtracking algorithm to solve the eight queen problem.

The eight Queens question, an old and famous problem, is a typical case of backtracking algorithms. The problem is the international chess player Max Bessel in 1848: Put eight queens on the 8x8 chess, so that they can not attack each other, that is, any two queens can not be in the same row, the same column or the same slash, ask how many kinds of pendulum.

The idea is to specify the Queen by line, the first line puts the first queen, the second row puts the second one, and then by traversing all the columns, to determine whether the next queen can be placed in the column. Not until all the queens have been put out, or put anywhere.

In more detail, the first Queen puts the first row first, then the second queen in the first column of the second row, and then determines whether it is OK, then the second column, the third column, then all the columns are finished, find a suitable, continue the third Queen, or the first column, the second column ... Until the 8th queen can be placed in a non-conflicting position, we find a correct solution. Then go back to the first queen to put the second column, back to continue the loop ...

Okay, start on the code.

Package huisu;/** * Created by Wolf on 2016/3/16.    */public class Wolfqueen {/** * total number of Queens (at this time set to 8 queens in the 8x8 chessboard, you can modify this value to set the N Queen problem) */int max = 8;    /** * The array saves the result, the first queen is placed in the array[0] column, the second in the array[1] column */int[] array = new Int[max];    public static void Main (string[] args) {new Wolfqueen (). Check (0); }/** * N represents the current number of Queens * @param n * Queen N in array[n] column */private void check (int n) {//Termination condition is the last line has been finished            , since every step of the pendulum checks if there is a conflict, so long as the last line is finished, the description has been given a correct solution if (n = = max) {print ();        Return            }//Start with the first column and then determine if there is a conflict with this line of this column, and if OK, enter the logical for (int i = 0; i < max; i++) {array[n] = i;            if (judge (n)) {check (n + 1); }}} private Boolean judge (int n) {for (int i = 0; i < n; i++) {if (array[i] = = Array [N] | |            Math.Abs (n-i) = = Math.Abs (Array[n]-array[i]) {return false;    }} return true; }   private void print () {for (int i = 0; i < Array.Length; i++) {System.out.print (Array[i] + 1 + "        ");    } System.out.println (); }}
The above is all code. Logic is also relatively simple, line by row, and then iterate through until the row is found to the appropriate column before entering the next line.



Backtracking algorithm solving eight Queens Problem (Java edition)

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.