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

58 篇文章 0 订阅
20 篇文章 0 订阅

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;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值