深搜+打表。
1.全局变量打表
2.出口判断
3.用check[i]=j代表第i行放置的是第j个棋子
#include<cstdio>
using namespace std;
int check[11]={0},s[11]={0};//下标是行数,值是第几个棋子,s储存答案
int ct=0,n=1;//n全局变量,用来打表
int dfs(int cur)
{
if(cur>n)
{
ct++;
return 0;
}
else
{
for(int i=1;i<=n;i++)
{
int ok=1;
check[cur]=i;
for(int j=1;j<cur;j++)
{
if(check[j]==check[cur]||cur-j==check[cur]-check[j]||cur+check[cur]==j+check[j])
{
ok=0;
break;
}
}
if(ok)
dfs(cur+1);
}
}
}
int main(void)
{
for(n=1;n<11;n++)
{
ct=0;
dfs(1);
s[n]=ct;
}
int k;
while(scanf("%d",&k)==1&&k!=0)
{
printf("%d\n",s[k]);
}
return 0;
}