Problem description placed n Queens on the N*n's checkered chessboard, 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 has several lines, one positive integer n≤10 per line, indicating the number of boards and queens, or, if n=0, the end.
Output has several 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
The direct request will time out, a table will not ...
#include <iostream> #include <cstdio> #include <cstring>using namespace Std;int a[20],ans,n,f[20]; void Dfs (int cnt) {if (cnt==n) {Ans++;return;} for (int i=0;i<n;i++) {int ok=1;a[cnt]=i;for (int j=0;j<cnt;j++) {if (A[cnt]==a[j] | | cnt-a[cnt]==j-a[j] | | cnt+a[ Cnt]==j+a[j]) {ok=0;break;}} if (OK) DFS (cnt+1);}} int main () {int i,j;for (n=1;n<=11;n++) {Ans=0;memset (a,0,sizeof (a));d FS (0); F[n]=ans;} while (scanf ("%d", &n) ==1 &&n) {Cout<<f[n]<<endl;} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 2553 N Queen problem (Dfs_ backtracking)