[C ++] Data Structure: Eight queens of algorithm analysis

Source: Internet
Author: User
/*************************************** * ******************************** // * Eight Queen's question, it is an ancient and famous question, and it is backtracking. Algorithm . /* This problem was raised by the famous mathematician Gauss in the 19th century in 1850:/* Eight queens were placed on 8x8 gags of chess, so that they could not attack each other, /* The two queens cannot be in the same row, column, or diagonal line. /*************************************** ******************************** // Use the position array storage Queen placement location. // For example, position [2] = 4 // indicates that the Queen of the 2nd rows is placed in the 4th column. // Traverse the 0th columns in sequence. // Code The position array is output each time the position is placed successfully for your understanding. // Enter Queen = 5 to check the placement principle. # Include <iostream> # include <math. h> # include <malloc. h> using namespace STD; int * position; // use the position of the array, P = position [I]: The queen of row I in column P int Queens; // The number of Queens int count; // the nth possibility // determine whether the nth row contains the Queen's bool signpoint (int n) {for (INT I = 0; I <N; I ++) {If (position [I] = position [N]) // This column has been placed in the Queen's return false; if (ABS (position [I]-position [N]) = n-I) // return false has been placed on the diagonal line;} return true ;} // place Queen's empress in column N // The default value is from column 0th. Void setqueen (INT n = 0) {If (Queens = N) // if they are equal, the result is arranged in the last column and the result is output to {cout <"no. "<++ count <Endl; For (INT I = 0; I <Queens; I ++) // traverse each row {for (Int J = 0; j <Queens; j ++) // traverse each column {If (j = position [I]) {cout <j <""; // if the number of columns is equal to the number of columns stored in position, output the number of columns} else {cout <"*"; // otherwise, output * Indicates not to put the Queen} cout <Endl ;}for (int t = 0; t <Queens; t ++) // output position Array {cout <"position [" <t <"] =" <position [T] <Endl ;}cout <Endl; return;} else // if not equal, place the Queen in this column {for (INT I = 0; I <Queens; I ++) // prepare to place the Queen in row I {position [N] = I; // place the Queen in row n, check if (signpoint (N) for the number of columns stored as I // {setqueen (n + 1) if the place is correct ); // continue placing the Queen in the next column }}} int main () {cout <"Enter the total number of Queens:" <Endl; CIN> Queens; position = new int [queens]; setqueen (); cout <"placement completed" <Endl; return 0 ;}

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.