291. Mondrian's Dream (Thinking + State Pressure dp)

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


Ideas:

A very important point is that after placing the legal horizontal blocks, the placement of the vertical blocks is determined.

So enumerate the status of each column, expressed in binary. j is the state of the previous column, and k is the state of the column.

It can be known that j&k==0 and the number of consecutive 0s in each segment of j|k is even.

Therefore, the state of conformity where the number of consecutive 0s in each segment of j|k is an even number is consistent in each column state. Just pre-process in advance.

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

 

Guess you like

Origin blog.csdn.net/zstuyyyyccccbbbb/article/details/115171072