Solution implementation of C ++ for eight queens (or N queens)

Source: Internet
Author: User
Tags bitset
/**//*********************************** ******************
* Cause: I saw someone in the C ++ community asking how to solve the eight queens (or N queens) problem in a simple and fast manner.
*
* Problem description: N * n chessboard, where n queens are not eating each other (any two queens have different rows and different columns with different 45 degrees oblique), and all combinations are output.
*
* Train of Thought: Use a narrow range search method: If you place a piece in Row 3, the place where the pieces can be placed in rows 3 to N is restricted and marked;
* If you can place the pawns in the second row, the places where the pawns can be placed in the second row are further restricted and marked;
* And so on. For example, if all rows are marked as not allowed, roll back to the previous row and re-release the child. Search for all feasible solutions.
*
* Algorithm: The bitset class provided by C ++ is used to make full use of bit operations to increase scalability.
* Use the array STS [N] to indicate the free location of each row (the location where the queen can be placed ).
* For example, the first behavior STS [0] = 111101... 11 indicates that the first row can be placed except for the fifth digit, And the rest cannot be placed (0 indicates that the row can be placed, and 1 indicates that the row cannot be placed)
* Obviously, if a row contains STS [x] = 1111111... 11 (full 1), this placement method fails and needs to be rolled back.
*
* Compiling environment: vc6.0
*
**************************************** ************/
# Include <iostream>
# Include <bitset>
Using namespace STD;

# Define N 8

Int POS [N];
Bitset <n> STS [N];
Bitset <n> mask [N] [N];
Int count;

/** // * Output result */
Void print ()...{
Int I, J;
Cout <Endl;
For (I = 0; I <n; I ++ )...{
For (j = 0; j <POS [I]; j ++) cout <".";
Cout <"O ";
For (j = POS [I] + 1; j <n; j ++) cout <".";
Cout <Endl;
}
}
/** // * Search for the first entry */
Void Queen (int n )...{
Int I, J;
If (~ STS [N] = 0) return;
For (I = 0; I <n; I ++ )...{
If (! STS [N]. Test (I ))...{
Pos [N] = I;
If (n + 1 = N)... {// set
Print (); count ++;
Return;
}
For (j = n + 1; j <n; j ++ )...{
Mask [N] [J] = STS [J];
STS [J]. Set (I );
If (I + J-n <n) Sts [J]. Set (I + J-N );
If (I + N-j> = 0) Sts [J]. Set (I + N-j );
}
Queen (n + 1 );
For (j = n + 1; j <n; j ++) Sts [J] & = mask [N] [J]; // rollback
}
}
}
Int main (INT argc, char * argv [])
...{
Int I, J;
Count = 0;
For (I = 0; I <n; I ++) Sts [I]. Reset ();
For (I = 0; I <n; I ++) for (j = 0; j <n; j ++) mask [I] [J]. Reset ();
Cout <"result is :";
Queen (0 );
Cout <"Total" <count <"sorting method" <Endl;
Return 0;
}

Execution result:

Related Article

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.