Description
N-Queens are placed in the N*n's checkered checkerboard, making them not attack each other (that is, any 2 queens are not allowed to be in the same row, in the same column, or in a diagonal line with a 45-angle checkerboard border.)
Your task is to find out how many legal placement methods are available for a given n.
Input
There are several lines, one positive integer n≤10 per line, indicating the number of boards and queens, or, if n=0, the end.
Output
A number of rows, one positive integer per line, representing the number of different placements of the queen corresponding to the input row.
Sample Input
1850
Sample Output
19210
Problem Solving Ideas:
is to consider the location of the Queen placement, for each row, we need to enumerate each can place the Queen's position, and need to determine whether the current position (line i) satisfies the condition, that is to determine whether this position and the place of the former I-1 row of the Queen's position conflict, if the conflict, the position is not appropriate; , you can enumerate the position of the next row of Queens until the nth row. Program code:
#include <iostream>#include<cstdio>using namespacestd;inta[ One],n,temp;voidDfsintk) { intI,j,flag; if(k==n+1) {Temp++; return ; } Else { for(i=1; i<=n;++i) {a[k]=i; Flag=1; for(j=1; j<k;++j) {if(a[j]==i| | i-k==a[j]-j| | i+k==a[j]+j) {Flag=0; Break; } } if(flag) DFS (k+1); } }}intMain () {intb[ One],m; for(n=1; n<=Ten;++N) {temp=0; DFS (1); B[n]=temp; } while(SCANF ("%d",&m) {printf ("%d\n", B[m]); } return 0;}
N Queen Placement method seed number