HDU 2553 N皇后问题

时间:2014-02-10 18:40:02   收藏:0   阅读:370

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#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;
}

原文:http://www.cnblogs.com/forever97/p/3543090.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!