Queen eight (3)

Source: Internet
Author: User

# 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;
}

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.