sgu 222 - Little Rooks 简单的状态压缩DP

     每行的状态用二进制数表示~~自己好些地方没细心~导致WA了很久..


Program:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<set>
#include<algorithm>
#include<cmath>
#define oo 1000000007
#define ll long long
#define pi acos(-1.0)
#define MAXN 505
using namespace std;  
ll dp[12][1026][12];
int canuse[15],w[15],num;
bool judge(int a)
{
      bool f=false;
      int x=a;
      w[num+1]=0;
      while (x) w[num+1]+=x%2,x/=2;
      while (a)
      {
            if (a%2 && f) return false;
            if (a%2) f=true;
            a/=2;
      }
      return true;     
} 
bool ok(int a,int b)
{
      while (a && b)
      {
            if (a%2 && b%2) return false;
            a/=2,b/=2;
      }
      return true;
}
int main()
{ 
      int n,k,t,i,j,x;
      ll ans;
      while (~scanf("%d%d",&n,&k)) 
      { 
             if (k>n) 
             {
                      printf("0\n");
                      continue;
             }
             memset(dp,0,sizeof(dp));
             num=0;
             for (i=0;i<(1<<n);i++)
               if (judge(i)) canuse[++num]=i; 
             dp[0][0][0]=1;
             for (t=1;t<=n;t++)
                for (i=1;i<=num;i++)
                   for (j=0;j<(1<<n);j++)
                      if (ok(canuse[i],j))
                        for (x=0;x<=k-w[i];x++)
                          dp[t][canuse[i]+j][x+w[i]]+=dp[t-1][j][x];
             ans=0;
             for (i=0;i<(1<<n);i++) ans+=dp[n][i][k];
             printf("%I64d\n",ans);             
      }
      return 0;
}



你可能感兴趣的:(sgu 222 - Little Rooks 简单的状态压缩DP)