#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;//主对角线上个元素行列之和相等,次对角线上个元素行列之差相等,为避免行列之差为负值,将结果加上N-1; dfs(1,i,j); } dp[j]=ans; } while(scanf("%d",&N)&&N!=0) { printf("%d\n",dp[N]); } return 0; }
时间: 2024-10-03 21:48:01