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;

}



你可能感兴趣的:(HDU)