# Include <iostream. h>
Class queen
{
Public:
Queen (INT x = 0, int y = 0 );
Void fill_up ();
Void printf ();
Int check (int x, int y );
Void place (int n );
Virtual ~ Queen ();
PRIVATE:
Int loc_x;
Int loc_y;
Char borad [8] [8];
};
Queen: Queen (int x, int y)
{
Loc_x = X;
Loc_y = y;
Fill_up ();
Borad [loc_x] [loc_y] = 'q ';
}
Void queen: fill_up ()
{
Int I, J;
For (I = 0; I <8; I ++)
For (j = 0; j <8; j ++)
Borad [I] [J] = 'X ';
Return;
}
Void queen: printf ()
{
Int I, J;
For (I = 0; I <8; I ++)
{For (j = 0; j <8; j ++)
Cout <borad [I] [J];
Cout <Endl;
}
Return;
}
Int queen: Check (int x, int y)
{
Int X1 = x, Y1 = y;
Int I;
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
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 queen: Place (int n)
{
Int I;
For (I = 0; I <8; I ++) // controls the location of the Queen in each row (number of columns)
{
If (check (n, I) // control the number of rows with the Queen number ~~~~ Check whether each column of this row can be placed with Q
{
Borad [N] [I] = 'q ';
If (n <7)
Place (n + 1); // when there are less than eight queens, the next one is recursion!
Else {
Printf (); // otherwise output the result
Return;
}
Borad [N] [I] = 'X'; // when the outermost for controls the column position,
// If there are no more than 8 backend entries, then Q is placed in the next position.
}
}
}
Queen ::~ Queen ()
{
Cout <"goodbye! "<Endl;
}
Int main ()
{
Queen equeen (0, 0 );
Equeen. Place (1 );
Return 0;
}