Description
在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上。
你的任务是,对于给定的N,求出有多少种合法的放置方法。
你的任务是,对于给定的N,求出有多少种合法的放置方法。
Input
共有若干行,每行一个正整数N≤10,表示棋盘和皇后的数量;如果N=0,表示结束。
Output
共有若干行,每行一个正整数,表示对应输入行的皇后的不同放置数量。
Sample Input
1 8 5 0
Sample Output
1 92 10#include <iostream> #include <stdio.h> const int maxn=11; int C[maxn]; int n,tot=0; using namespace std; void search(int cur) { if(cur==n) {tot++; return;} else { for(int i=0;i<n;i++) { int ok=1; C[cur]=i; for(int j=0;j<cur;j++){ if(C[cur]==C[j]||cur-C[cur]==j-C[j]||cur+C[cur]==j+C[j]) { ok=0;break; } } if(ok) search(cur+1); } } } int main() { int a[12],m; for(n=1;n<=10;n++) { tot=0; search(0); a[n]=tot; } while(scanf("%d",&m)&&m) { printf("%d\n",a[m]); } return 0; }