http://acm.hdu.edu.cn/showproblem.php?pid=2553 #include<iostream> using namespace std; #define N 10 int n; int ans; bool mark[N][N]; bool judge(int h,int x) { int hh=h-1; int ll=x-1; while(hh>=0&&ll>=0) if(mark[hh--][ll--]) return false; hh=h-1; ll=x+1; while(hh>=0&&ll<n) if(mark[hh--][ll++]) return false; for(int i=0;i<h;i++) if(mark[i][x]) return false; return true; } void dfs(int h,int count) { if(count==n) { ans++; return; } for(int j=0;j<n;j++) { if(judge(h,j)) { mark[h][j]=1; dfs(h+1,count+1); mark[h][j]=0; } } } int re[N]; int main(void) { for(int i=1;i<=N;i++) { ans=0; n=i; for(int j=0;j<n;j++) { memset(mark,0,sizeof(mark)); mark[0][j]=1; dfs(1,1); } re[i-1]=ans; } while(scanf("%d",&n),n) printf("%d/n",re[n-1]); }