/*************************************** * ******************************** // * 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 ;}