Classic backtracking algorithm (eight Queen's question) detailed

Source: Internet
Author: User

Eight Queen's problem is an old and famous problem, which is a typical example of backtracking algorithm. The issue was 19th century by the famous mathematician Gauss 1850 presented:

Put eight queens on the 8x8 chess, so that they cannot attack each other, that is, any two queens cannot be on the same line, the same column, or the same slash

(slope is 1), ask how many kinds of pendulum method. Gauss thinks there are 76 kinds of schemes.

In 1854, in the Chess magazine in Berlin, different authors published 40 different solutions, and later some of them solved 92 kinds of results using the method of graph theory.

After the invention of computer, there are many ways to solve this problem.

Algorithm ideas:

First we analyze the solution of the problem, we take out a queen, put in a row, a total of eight different methods,

And then put the second queen, also if the rule is not considered, there are eight ways to put the law.

So we can describe the process with an eight-pronged tree. Starting from the root node, each additional layer of the tree, is to put a more queen,

Until the 8th level (the root node is 0 layers), finally get a complete eight-fork tree.

Then we begin to traverse the eight-fork tree with depth first, and in the process of traversal, the corresponding conditions are judged. To remove the irregular subtree.

So what is the specific condition for the sub-tree cutting?

Let's first make a pact about the structure of the problem solution.

X[i], in line I, the Queen placed in the x[i] this position.

so we consider the first condition, can not be the same row, the same column so we get x[i] cannot be the same.

The remaining condition is not on the diagonal, this condition is not very obvious, we have analyzed,

Set two different queens on the J,k line, X[j],x[k, respectively, on the column of the J,k row.

Then the conditions that are not on the same diagonal can be written as ABS ((J-K))!=abs (X[j]-x[k]), where ABS is a function of absolute value.

#include <iostream>using namespace Std;int num;int *x;int sum;bool Place (int k) {for    (int j = 1;j<k;j++)        if (ABS (X[k]-x[j]) = = = ABS (k-j) | | X[J] = = X[k])            return false;        return true;} void backtrack (int t) {    if (t>num)//num is the number of Queens    {        sum++;//sum for all feasible solutions for        (int m = 1;m<=num;m++)        {            cout<< "<" <<m<< "," <<x[m]<< ">";//This line is output when recursive to the leaf node, a feasible solution        }        cout<<endl;    }    else for        (int i = 1;i<=num;i++)        {            x[t] = i;            if (place (t)) backtrack (t+1);//Here is used to make the judgment of the condition we said above, if it is established, go to the next level of recursion        }}void main () {    num = 8;    sum = 0;    x = new Int[num+1];    for (int i= 0;i<=num;i++)        x[i] = 0;    Backtrack (1);    cout<< "scheme in common" <<sum<<endl;delete []x;    }


Classical backtracking algorithm (eight Queen's question) detailed

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.