#include <iostream> #include <cstdio> #include <cstring>using namespace std;const int Max_n=100;int N; int Vis[3][max_n];int ans;int dp[max_n];void dfs (int r, int c,int N) {if (r==n) {Ans++;return;} for (int i=0; i<n; i++) {if (!vis[0][i]&&!vis[1][r+i]&&!vis[2][r-i+n-1]) {vis[0][i]=vis[1][r+i]= Vis[2][r-i+n-1]=1;dfs (r+1,i,n); vis[0][i]=vis[1][r+i]=vis[2][r-i+n-1]=0;}} int main () {for (int j=1; j<=10; J + +) {ans=0;for (int i=0; i<j; i++) {memset (Vis, 0, sizeof (VIS)); vis[0][i]=vis[1][0+i ]=vis[2][0-i+n-1]=1;//the sum of the columns of the last element of the main diagonal, the difference between the ranks of the last element of the second diagonal is equal, and the result is added N-1;dfs (1,i,j) to avoid the difference between the columns. Dp[j]=ans;} while (scanf ("%d", &n) &&n!=0) {printf ("%d\n", Dp[n]);} return 0;}
HDOJ2553 (2N Queen question)