hdu 2553 八皇后问题 基础

来源:互联网 发布:linux中ll命令 编辑:程序博客网 时间:2024/05/26 17:44

题意:给你一个n*n的棋盘,要求放n个皇后;

<span style="font-size:18px;">#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <vector>#include <queue>#include <map>#include <algorithm>#include <set>using namespace std;typedef long long ll;const double eps = 1e-10;const int inf = 0x3f3f3f3f;const double pi=acos(-1);const int mod=100000000;int max(int a,int b){return a>b?a:b;};int min(int a,int b){return a<b?a:b;};int n,l[12],lh[102],hl[102],ans[12];void dfs(int m,int p){    if(m==n-1) {ans[n]++;return;}//能够取到最后一层直接加;     for(int j=0;j<n;j++)         if(!l[j]&&!lh[j+m+1]&&!hl[n-1-j+m+1])          {              l[j]=lh[j+m+1]=hl[n-1-j+m+1]=1;              dfs(m+1,j);              l[j]=lh[j+m+1]=hl[n-1-j+m+1]=0;          }}int main(){   memset(ans,0,sizeof(ans));   ans[1]=1;   for(n=2;n<=10;n++)   {       memset(l,0,sizeof(l));       memset(lh,0,sizeof(lh));       memset(hl,0,sizeof(hl));       for(int j=0;j<n;j++)          {              l[j]=lh[j]=hl[n-1-j]=1;              dfs(0,j);              l[j]=lh[j]=hl[n-1-j]=0;          }   }   while(~scanf("%d",&n)&&n)     printf("%d\n",ans[n]);   return 0;}</span>



0 0