其他分享
首页 > 其他分享> > 291. 蒙德里安的梦想(思维+状压dp)

291. 蒙德里安的梦想(思维+状压dp)

作者:互联网

https://www.acwing.com/problem/content/description/293/


思路:

一个很重要的点就是放好合法的横块之后,竖块的摆放就确定了。

于是枚举每一列的状态,用二进制表示。j为上一列的状态,k为此列的状态。

可以知道,j&k==0 并且j|k的每段连续0的个数是偶数是符合的。

于是j|k的每段连续0的个数是偶数的符合的状态在每一个列的状态是一致的。提前预处理就好。

if( (j&k)==0 &&(st[j|k] )

dp[i][j]+=dp[i-1][k]

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=14;
typedef int LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
long long dp[maxn][1<<maxn];
bool st[1<<maxn];
int main(void)
{
  LL n,m;
  while(scanf("%d%d",&n,&m)&&n!=0&&m!=0){

      for(LL i=0;i<(1<<n);i++){
         st[i]=true;
         LL cnt=0;///记录1前的连续0的个数
         for(LL j=0;j<n;j++){
             if(i&(1<<j)){
                if(cnt&1){
                    st[i]=false;break;
                }
                cnt=0;
             }
             else cnt++;
         }
         if(cnt&1) st[i]=false;///下面只有0的
      }

      memset(dp,0,sizeof(dp));
      dp[0][0]=1;
      for(LL i=1;i<=m;i++){///枚举每一列
          for(LL j=0;j<(1<<n);j++){
              for(LL k=0;k<(1<<n);k++){
                  if((j&k)==0&&(st[j|k]==true)){
                     dp[i][j]+=dp[i-1][k];
                  }
              }
          }
      }
      printf("%lld\n",dp[m][0]);
  }
return 0;
}

 

标签:状态,蒙德里安,状压,偶数,一列,每段,include,dp
来源: https://blog.csdn.net/zstuyyyyccccbbbb/article/details/115171072