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