1 8 5 0
1 92 10
#include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> using namespace std; const int MAX = 15; bool col[MAX+1],lx[MAX],ly[MAX]; int ans[MAX] = {0,1,0,0,2,10,4,40,92,352,724}; void dfs(int x,int limit){ int i; if(x>limit){ ++ans[limit]; return; } for(i=1;i<=limit;++i){ if(col[i])continue; if(lx[x-i+limit-1])continue; if(ly[x+i])continue; lx[x-i+limit-1] = true; ly[x+i] = true; col[i] = true; dfs(x+1,limit); col[i] = false; lx[x-i+limit-1] = false; ly[x+i] = false; } } void init(){ int i; for(i=1;i<=MAX;++i){ col[i] = false; lx[MAX] = false; ly[MAX] = false; ans[i] = 0; } for(i=1;i<=10;++i){ dfs(1,i); } } int main(){ //freopen("in.txt","r",stdin); //(author : CSDN iaccepted) //init(); int n; while(scanf("%d",&n)){ if(n==0)break; printf("%d\n",ans[n]); } return 0; }
HDU 2553 N皇后问题 (搜索DFS),布布扣,bubuko.com
原文地址:http://blog.csdn.net/iaccepted/article/details/26172471