N皇後問題
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): Accepted Submission(s):
Problem Description
在N*N的方格棋盤放置了N個皇後,使得它們不相互攻擊(即任意2個皇後不允許處在同一排,同一列,也不允許處在與棋盤邊框成45角的斜線上。
你的任務是,對於給定的N,求出有多少種合法的放置方法。
Input
共有若干行,每行一個正整數N≤10,表示棋盤和皇後的數量;如果N=0,表示結束。
Output
共有若干行,每行一個正整數,表示對應輸入行的皇後的不同放置數量。
Sample Input
1
8
5
0
Sample Output
1
92
10
n皇後問題,不知道怎麼,深搜回溯還是會超時,打表有時是很吊的!
/*#include
#include
#include
#define N 20
int n,s,a,l[N];
int kefang(int a)
{
int j;
for(j=1;jn&&n>0)
s++;
else{
for(i=1;i<=n;i++)
{
l[a]=i;
if(kefang(a))
xiaqi(a+1);
}
}
return s;
}
int main()
{
while(~scanf("%d",&n)&&n)
{
int num;
s=0;
num=xiaqi(1);
printf("%d\n",num);
}
return 0;
}*/
#include
using namespace std;
int main()
{
int n;
int a[11]={0,1,0,0,2,10,4,40,92,352,724};
while(cin>>n)
{
if(n==0)
break;
cout<