On an 8x8 international chess board, a maximum of five queens can be placed to control the entire situation, that is, no matter which one of the other's pawns is placed, it will be eaten and the distribution of five Queens should be compiled.

Source: Internet
Author: User

# Include <stdio. h>
# Include <fstream>
# Include <map>
# Include <set>

Using namespace STD;

# Include <stdio. h>
Typedef struct ctrlpoint
{
Int ncount;
Set <int> value;
} Ctrlpoint;

Int end [5] = {59,60, 61,62, 63 };
Int begin [5] = {0, 1, 2, 3, 4 };
Ctrlpoint ctlpoint [64] = {0 };
Bool getnextvalue (int * pdata, int POS)
{
Pdata [POS] ++;
If (pdata [POS] <= end [POS])
Return true;
Int nnext = pos-1;
While (nnext> = 0)
{
Pdata [nnext] ++;
If (pdata [nnext] <= end [nnext])
{
For (INT I = nnext; I <Pos; I ++)
Pdata [I + 1] = pdata [I] + 1;
Return true;
}
Else
-- Nnext;
}

Return false;
}
Void getctrlpoint (INT num, ctrlpoint * pctrl)
{
Int x = num/8;
Int y = num % 8;
Int I = 0;
Int J = 0;
Pctrl-> ncount = 0;
For (INT I = 0; I <8; I ++)
{
Pctrl-> value. insert (x * 8 + I );
Pctrl-> ncount ++;
If (I = X)
Continue;
Pctrl-> value. insert (I * 8 + y );
Pctrl-> ncount ++;

}
I = x + 1;
J = Y + 1;
For (; I <8 & J <8; I ++, J ++)
{
Pctrl-> value. insert (I * 8 + J );
Pctrl-> ncount ++;
}
I = x + 1;
J = Y-1;
For (; I <8 & J> = 0; I ++, j --)
{
Pctrl-> value. insert (I * 8 + J );
Pctrl-> ncount ++;

}
I = X-1;
J = Y-1;
For (; I> = 0 & J> = 0; I --, j --)
{
Pctrl-> value. insert (I * 8 + J );
Pctrl-> ncount ++;

}

I = X-1;
J = Y + 1;
For (; I> = 0 & J <8; I --, J ++)
{
Pctrl-> value. insert (I * 8 + J );
Pctrl-> ncount ++;

}

}

Bool cancover ()
{
Set <int> pointset;
For (INT I = 0; I <5; I ++)
{
Ctrlpoint Ppoint = ctlpoint [begin [I];
// For (Int J = 0; j <Ppoint. ncount; j ++)
Pointset. insert (Ppoint. value. Begin (), Ppoint. value. End ());

}

If (pointset. Size ()> = 64)
{
Printf ("% d, total = % d \ n", begin [0], begin [1], begin [2], begin [3], begin [4], pointset. size ());
Return true;
}
Return false;
}

Int main ()
{
For (INT I = 0; I <64; I ++)
{
Getctrlpoint (I, & ctlpoint [I]);
}
While (1)
{
Cancover ();

If (! Getnextvalue (begin, 4 ))
Break;
}
Getchar ();

}

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.