// ================= Program description ======================================= ================ //
// Program file: queeen. cpp //
// Program name: Eight queens question //
// Method: Non-recursive method //
// Program: (exceedstudio)
// ================================================ ========================================= //
# Include <iostream. h>
Char borad [8] [8];
Bool issucceed = false;
Int main ()
{
Void fill_up ();
Void place (); // enter each queen
Int check (INT, INT); // checks whether Q exists on each row, column, and primary/secondary diagonal lines.
Int x = 0, Y; // position
Char answer;
Int I, j; // control the loop
Fill_up ();
K: do {
Cout <"place the first queen (1------8) [only in the first line!] "<Endl;
Cin> Y;
If (Y <= 0 | Y> 8)
Cout <"the number is out of range! Please try again! "<Endl;
} While (Y <= 0 | Y> 8 );
Borad [x] [Y-1] = 'q ';
Place ();
If (! Issucceed)
{
Cout <"in this place can not match the order! "<Endl <" The programme is end! "<Endl;
Cout <"go on? (Y/N (or other) "<Endl;
Cin> answer;
If (answer = 'y' | answer = 'y ')
{
Fill_up ();
Goto K;
}
}
Else {
Issucceed = false;
Cout <"the Queen on the borad is:" <Endl;
Cout <"-------------------------" <Endl;
Cout <"go on? (Y/N (or other) "<Endl;
Cin> answer;
If (answer = 'y' | answer = 'y ')
{For (I = 0; I <8; I ++)
For (j = 0; j <8; j ++)
Borad [I] [J] = 'X ';
Goto K;
}
}
// Check the correctness of each Queen (ensure there is no Q on the diagonal lines of the row and column)
For (I = 0; I <8; I ++)
For (j = 0; j <8; j ++)
If (borad [I] [J] = 'q ')
If (check (I, j ))
Cout <"wrong! "<Endl;
Return 1;
}
Void fill_up ()
{
Int I, J;
For (I = 0; I <8; I ++)
For (j = 0; j <8; j ++)
Borad [I] [J] = 'X ';
}
Int check (int x, int y)
{
Int I, X1 = x, Y1 = y;
For (I = 0; I <8; I ++)
If (borad [x] [I] = 'q' | borad [I] [Y] = 'q ')
Return 0; // judge whether there is no Q in the row or column
// Check whether Q exists in the diagonal line
While (X1 <8 & Y1 <8)
{
If (borad [X1 + 1] [Y1 + 1] = 'q ')
Return 0;
Else {
X1 ++;
Y1 ++;
}
}
X1 = X;
Y1 = y;
While (x1> 0 & Y1> 0)
{
If (borad [x1-1] [y1-1] = 'q ')
Return 0;
Else {
X1 --;
Y1 --;
}
}
X1 = X;
Y1 = y;
While (x1> 0 & Y1 <8)
{
If (borad [x1-1] [Y1 + 1] = 'q ')
Return 0;
Else {
X1 --;
Y1 ++;
}
}
X1 = X;
Y1 = y;
While (X1 <8 & Y1> 0)
{
If (borad [X1 + 1] [y1-1] = 'q ')
Return 0;
Else {
X1 ++;
Y1 --;
}
}
Return 1;
}
Void place ()
{
Int x1, x2, X3, X4, X5, X6, X7;
Int I, J;
For (x1 = 0; X1 <8; X1 ++)
{
If (check (1, X1 ))
Borad [1] [X1] = 'q ';
Else
Continue;
For (x2 = 0; X2 <8; x2 ++)
{
If (check (2, X2 ))
Borad [2] [X2] = 'q ';
Else
Continue;
For (X3 = 0; X3 <8; X3 ++)
{
If (check (3, X3 ))
Borad [3] [X3] = 'q ';
Else
Continue;
For (X4 = 0; X4 <8; X4 ++)
{
If (check (4, X4 ))
Borad [4] [X4] = 'q ';
Else
Continue;
For (X5 = 0; X5 <8; X5 ++)
{
If (check (5, X5 ))
Borad [5] [X5] = 'q ';
Else
Continue;
For (X6 = 0; X6 <8; X6 ++)
{
If (check (6, X6 ))
Borad [6] [X6] = 'q ';
Else
Continue;
For (X7 = 0; X7 <8; X7 ++)
{
If (check (7, X7 ))
{
Borad [7] [X7] = 'q ';
For (I = 0; I <8; I ++)
{
For (j = 0; j <8; j ++)
Cout <borad [I] [J];
Cout <Endl;
}
Issucceed = true;
}
}
Borad [6] [X6] = 'X ';
}
Borad [5] [X5] = 'X ';
}
Borad [4] [X4] = 'X ';
}
Borad [3] [X3] = 'X ';
}
Borad [2] [X2] = 'X ';
}
Borad [1] [X1] = 'X ';
}
}