2n Queen question

Source: Internet
Author: User

Part I: Topics

Problem description Given a n*n board, there are some places in the chessboard that cannot be placed on the Queen. Now to put n black Queen and n White Queen in the chessboard, so that any two black queens are not on the same line, the same column or the same diagonal, any two white queens are not on the same line, the same column or the same diagonal. Q. How many kinds of methods are there in total? n is less than or equal to 8. Enter the first behavior of the input format an integer n that represents the size of the checkerboard.
Next n rows, each row n 0 or 1 of integers, if an integer is 1, indicating that the corresponding position can be placed Queen, if an integer is 0, indicating that the corresponding position can not put the queen. The output format outputs an integer that indicates how many methods are put in total. Sample Input 4
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1 Sample Output 2 Example input 4
1 0 1 1
1 1 1 1
1 1 1 1
1 1 1 1 sample output 0 Part II: Code
/*1, each line must put a black queen, a white queen. 2, from the first line to the last line, first put the Black queen, and then put the White queen. (Of course, not only this method) 3, put the attention is 0 of the place can not put, let go of place. (This thought is on the basis of the N-Queen directly is ordered to select two total, not: Different methods may have the same point of use)*/#include<iostream>using namespacestd;ints[ -][ -];intN;intCount=0;voidDfsintIintq) {     for(intj=0; j<n;j++)    {        //I can't put it on or leave it.        if(s[i][j]==0|| s[i][j]==2)        {            Continue; }        intflag=1;//default can be put        inty1=j-1; inty2=j+1;  for(intl=i-1; l>=0; l--)        {            //judge whether the same column, slash on the same queen (peer certainly will not have: from top to bottom)//same column            if(s[l][j]==q) {flag=0;  Break; }            //Slash            if(y1>=0&&s[l][y1]==q) {flag=0;  Break; } y1--; if(y2<n&&s[l][y2]==q) {flag=0;  Break; } y2++; }        if(flag) {S[i][j]=q;//put the Queen            if(i<n-1) {DFS (i+1, q); }             Else            {                //The Black queen has finished, and began to put the White queen;//when the White Queen finishes, it's a way to end.                if(q==2) {DFS (0,3); }                Else{Count++; }} S[i][j]=1;//recovery starts next time        }    }}intMain () {CIN>>N;  for(intI=0; i<n;i++)    {         for(intj=0; j<n;j++) {cin>>S[i][j]; }} DFS (0,2);//Black Queencout<<count<<Endl; return 0;}

2n Queen question

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.