sgu 223 - Little Kings 状态压缩DP

   裸状态DP....dp[n][k]...n表示当前的状态...k表示已经放好的King数量  


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][150][105];
int canuse[150],w[150],num,n,k;
bool legal(int x)
{
      bool f=false;
      int p=x;
      w[num+1]=0; 
      while (p) w[num+1]+=p%2,p/=2; 
      while (x)
      {
            if (x%2 && f) return false;
            if (x%2) f=true;
               else f=false;
            x/=2;
      }
      return true;
}
bool ok(int a,int b)
{ 
      int s2[12],i;
      memset(s2,0,sizeof(s2)); 
      for (i=1;i<=n;i++) s2[i]=b%2,b/=2;
      for (i=1;i<=n;i++)
      {
             if (a%2 && (s2[i] || s2[i-1] || s2[i+1]))  return false;
             a/=2;
      }
      return true;
}
int main()
{
      int t,i,j,x;
      ll ans; 
      while (~scanf("%d%d",&n,&k))
      {
            num=0;
            for (i=0;i<(1<<n);i++)
               if (legal(i)) canuse[++num]=i;
            memset(dp,0,sizeof(dp));
            dp[0][1][0]=1;
            for (t=1;t<=n;t++)
               for (i=1;i<=num;i++)
                 for (j=1;j<=num;j++)
                   if (ok(canuse[i],canuse[j]))
                     for (x=0;x<=k-w[i];x++)
                       dp[t][i][x+w[i]]+=dp[t-1][j][x];
            ans=0;
            for (i=1;i<=num;i++) ans+=dp[n][i][k];
            printf("%I64d\n",ans);
      }
      return 0;
}


你可能感兴趣的:(sgu 223 - Little Kings 状态压缩DP)