Retrospective approach to solve eight imperial problems

Source: Internet
Author: User

Put eight queens on a 8*8 board, requiring the same row, the same column, the same diagonal cannot have two queens.

Ideas:
The key is to determine whether the two queens are in the same row, the same column, or the same
On the diagonal. Here, the checkerboard subscript is counted from 1 onwards.
Observations revealed:
If on the same line, the line number is the same;
If in the same column, the column number is the same;
If the same "/" diagonal, the row and column values are the same;
If you are on the same "\" diagonal, the difference between the row and column values is the same.
Given that there is only one queen per line, set the one-dimensional array a[1...8] to represent the Queen's position:
Row i, column J, place the Queen, then A[i]=j, that is, the subscript is the number of rows, the value is the number of columns.
Judge whether the Queen is safe, that is, check the same column, the same diagonal whether there is a queen.
Create a flag array b[1...8] can control the same column can only have one queen. If two Queens
On the same diagonal, the sum or difference of its row and column values is equal, so a flag array can also be established
C[1...16],D[-7..7] control can only have one queen on the same diagonal. Note
The C and D arrays here correspond to the main and vice 16 diagonal lines.)
If the slash is not in the direction, then the difference between the line number of the two queens on the same slash is the absolute value and the column number
The absolute value of the difference is equal. In this way, to represent two queens I and J not in
Conditions on the same column or slash can be described as: (A[i]!=a[j]) && (ABS (A[I]-A[J)))
I and J represent the line number of the two Queens respectively.

The specific code is as follows:

1#include <stdio.h>2#include <stdlib.h>3 intN;4 intsum=0, a[ -];//sum represents the total number of scenarios. A[i]=x indicates that row i is placed in the X column5 intb[ -]={0},c[ -]={0},d[ -]={0};6 //b[i]=1 that there is a queen in column I. 7 //C[i]=1 represents a queen on the sub-diagonal ("/" diagonal) of the I-bar from top left to bottom right8 //d[i]=1 means that there is a queen on the main diagonal ("\" diagonal) on the top right-to-left order of article I9 intSearchinti);//recursive backtracking placement of the Empress I (on line i)Ten voidPrint ();//Output Scenarios One intMain () A { -n=8;//the order of the Queen's question (number of Queens) -Search1); the     if(sum==0) printf ("No solution! \ n"); -     return 0; - } - intSearchintI//recursive backtracking placement of the Empress I (on line i) + { -     intJ; +      for(j=1; j<=n;j++) A     { at         if((!b[j]) && (!c[i+j]) && (!d[i-j+7])) -         { -a[i]=J; -b[j]=1; c[i+j]=1; d[i-j+7]=1; -             if(i==n) print ();//N Queen has been placed to complete, print placement scheme -             ElseSearch (i+1);//continue to place the next queen recursively . inb[j]=0; c[i+j]=0; d[i-j+7]=0;//restore the scene, the queen of the current location cleared -         } to     } + } - voidprint () the { *     inti; $sum++;Panax Notoginsengprintf"sum=%d\n", sum); -      for(i=1; i<=n;i++) theprintf"%-4d", A[i]); +printf"\ n"); A}

PS: Personally feel that the code inside the symbol array B, C, d with very clever.

The above code is more than two arrays of C, D simplifies the judgment. If only an array B can be used to judge whether the Queen will conflict, but the time efficiency is reduced a lot. The code is as follows:

1#include <stdio.h>2 intN;3 intsum=0, a[ -];//sum represents the total number of scenarios. A[i]=x indicates that row i is placed in the X column4 intb[ -]={0};//b[i]=1 that there is a queen in column I. 5 6 intSearchinti);//recursive backtracking placement of the Empress I (on line i)7 voidPrint ();//Output Scenarios8 intMain ()9 {Tenn=8;//the order of the Queen's question (number of Queens) OneSearch1); A     if(sum==0) printf ("No solution! \ n"); -     return 0; - } the intSearchintI//recursive backtracking placement of the Empress I (on line i) - { -     intj,k; -      for(j=1; j<=n;j++) +     { -         if(b[j]==0) +         { A              for(k=1; k<i;k++)//Scan a[] before i-1, check before I-1 Queen will clash with the first Queen at             { -                 if(K+A[K]==I+J) Break; -                 Else if(K-A[K]==I-J) Break; -             } -             if(k==i)//The former i-1 queen does not clash with the first Queen -             { ina[i]=J; -b[j]=1; to                 if(i==n) print ();//N Queen has been placed to complete, print placement scheme +                 ElseSearch (i+1);//continue to place the next queen recursively . -b[j]=0;//restore the scene, the queen of the current location cleared the             } *         } $     }Panax Notoginseng } - voidprint () the { +     inti; Asum++; theprintf"sum=%d\n", sum); +      for(i=1; i<=n;i++) -printf"%-4d", A[i]); $printf"\ n"); $}
View Code

Retrospective approach to solve eight imperial problems

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.