Mondriaan‘s Dream poj-2411 铺瓷砖 状压dp 寒假集训

Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after producing the drawings in his ‘toilet series’ (where he had to use his toilet paper to draw on, for all of his paper was filled with squares and rectangles), he dreamt of filling a large rectangle with small rectangles of width 2 and height 1 in varying ways.
在这里插入图片描述

Expert as he was in this material, he saw at a glance that he’ll need a computer to calculate the number of ways to fill the large rectangle whose dimensions were integer values, as well. Help him, so that his dream won’t turn into a nightmare!
Input
The input contains several test cases. Each test case is made up of two integer numbers: the height h and the width w of the large rectangle. Input is terminated by h=w=0. Otherwise, 1<=h,w<=11.
Output
For each test case, output the number of different ways the given rectangle can be filled with small rectangles of size 2 times 1. Assume the given large rectangle is oriented, i.e. count symmetrical tilings multiple times.
Sample Input
1 2
1 3
1 4
2 2
2 3
2 4
2 11
4 11
0 0
Sample Output
1
0
1
2
3
5
144
51205
这里建议看y总的视频讲解,自己刚学也讲不清楚,放一个自己的代码和y总的代码
自己的

#include<map>
#include<stack>
#include<queue>
#include<string>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define ls (k<<1)
#define rs (k<<1|1)
#define pb push_back
#define mid ((l+r)>>1)
using namespace std;
const int p=1e4+7;
const int mod=10007;
const int maxn=1500;
typedef long long ll;
const int inf=0x3f3f3f3f;   
const int N=12,M=1<<N;
ll dp[N][1<<N];//行 状态
int sta[M];//所有连续的0是否有偶数个
int n,m,cnt;
bool check(int a)
{
    
    
    int cnt=0;
    int tmp=m;
    while(tmp--){
    
    
        if(a&1){
    
    
            if(cnt%2!=0){
    
    
                return false;
            }
            cnt=0;
            a>>=1;
            continue;
        }
        cnt++;
        a>>=1;
    }
    if(cnt&1){
    
    
        return false;
    }
    return true;
}
void solve(){
    
    
    
    while(scanf("%d %d",&n,&m)!=EOF){
    
    //n 行,m列
        if(n==0&&m==0) break;
        memset(dp,0,sizeof(dp));
        for(int j=0;j<(1<<m);j++){
    
    //0是横着,1是竖着
            if(check(j)) 
                dp[1][j]=1;
        }
        for(int i=2;i<=n;i++){
    
    
            for(int j=0;j<1<<m;j++){
    
    //本层
                for(int k=0;k<1<<m;k++){
    
    //上层
                    if(dp[i-1][k]&&(j&k)==0&&check(j|k))//
                        dp[i][j]+=dp[i-1][k];
                }
            }
        }
        cout<<dp[n][0]<<endl;
    }
}
int main(){
    
    
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    solve(); 
    return 0;
}

y总的

#include<map>
#include<stack>
#include<queue>
#include<string>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define ls (k<<1)
#define rs (k<<1|1)
#define pb push_back
#define mid ((l+r)>>1)
using namespace std;
const int p=1e4+7;
const int mod=10007;
const int maxn=1500;
typedef long long ll;
const int inf=0x3f3f3f3f;   
const int N=12,M=1<<N;

ll dp[N][1<<N];//行 状态
int sta[M];//所有连续的0是否有偶数个
void solve(){
    
    
    int n,m;
    while(scanf("%d %d",&n,&m)!=EOF){
    
    //n 行,m列
        if(n==0&&m==0) break;
       
        for(int j=0;j<1<<n;j++){
    
    //0是横着,1是竖着
            int cnt=0;
            sta[j]=1;
            for(int i=0;i<n;i++){
    
       
                if(j>>i&1){
    
    //1
                    if(cnt%2==1) sta[j]=0;
                    cnt=0;
                }
                else cnt++;            
            }
            if(cnt%2==1) sta[j]=0;
        }
        memset(dp,0,sizeof(dp));
        dp[0][0]=1;
        for(int i=1;i<=m;i++){
    
    
            for(int j=0;j<1<<n;j++){
    
    //本层
                for(int k=0;k<1<<n;k++){
    
    //上层
                    if((j&k)==0&&(sta[j|k]))//
                        dp[i][j]+=dp[i-1][k];

                }
            }
        }
        cout<<dp[m][0]<<endl;
    }

}
int main(){
    
    
    solve();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_45891413/article/details/112756567
今日推荐