http://acm.hdu.edu.cn/showproblem.php?pid=2553
#include <bits/stdc++.h>
using namespace std;
int sum,n,a[15],b[15];
void dfs(int x){
if(x==n)
sum++;
else{
for(int i=0;i<n;i++){//n为列数
int ok=1;
a[x]=i;
for(int j=0;j<x;j++){
if(a[x]==a[j]||x-a[x]==j-a[j]||x+a[x]==j+a[j]){
ok=0;break;
}
}
if(ok) dfs(x+1);
}
}
}
int main(){
ios::sync_with_stdio(false);
for(int i=1;i<=10;i++){//打表,不然超时
n=i;
memset(a,0,sizeof(a));
sum=0;
dfs(0);
b[i]=sum;
}
while(cin>>n&&n)
cout<<b[n]<<endl;
return 0;
}
255

被折叠的 条评论
为什么被折叠?



