HDU 2553 N皇后问题

题解:http://www.matrix67.com/blog/archives/266

#include <cstdio>
int n,ans,upperlim;
void test(int row,int ld,int rd)
{
    int pos,p;
    if (row!=upperlim) 
    {
       pos=upperlim&(~(row|ld|rd));
       while(pos) 
       {
          p=pos&(~pos+1);
          pos=pos-p;
          test(row|p,(ld|p)<<1,(rd|p)>>1);
       }
    }
    else ans++;
}
int main()
{
    int i;
    while(scanf("%d",&n)&&n)
    {
        upperlim=(1<<n)-1;
        ans=0; test(0,0,0);
        printf("%d\n",ans);
    }
    return 0;
}

posted @ 2014-02-10 16:08  forever97  阅读(142)  评论(0编辑  收藏  举报