1295 N Queen Question

Source: Internet
Author: User

1295 N Queen question

time limit: 2 sspace limit: 128000 KBtitle level: Golden Gold SolvingTitle Description Description

Place the N-Queens that are not attacked by each other on the board of the NXN grid. According to the rules of chess, the Queen can attack pieces that are on the same line or in the same column or on the same slash. n the problem is equivalent to placing n queens on the re-NXN board, any 2 queens may be on the same row or column or on the same slash.

Enter a description Input Description

Size of a given checkerboard N (n≤13)

Output description Output Description

The output integer indicates how many placement methods are available.

Sample input Sample Input

8

Sample output Sample Output

92

Data range and Tips Data Size & Hint

N<=13

(The time limit has been improved, no need to make a watch)

Category labels Tags Click here to expandSearch Code:
#include <cstdio>using namespacestd;#defineN 101intA[n],b[n],c[n],d[n],n,tot;voidGointi) {     for(intj=1; j<=n;j++) {//looking for the place to put the Queen        if(!b[j]&&!c[i+j]&&!d[i-j+n-1]){//looking for the place to put the Queen//because C + + cannot manipulate negative groups, consider adding n-1A[i]=j;//placing the Queenb[j]=1;//Declaration of Occupation of Column Jc[i+j]=1;//occupy two diagonald[i-j+n-1]=1; if(i==n) tot++;//N All queens are placed, one case            ElseGo (i+1);//continue to place the next queen recursively .b[j]=0;//recursive return is a step back, the current queen quitsc[i+j]=0; D[i-j+n-1]=0; }    }}intMain () {scanf ("%d",&N); Go (1); printf ("%d\n", tot); return 0;}

1295 N 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.