1. 程式人生 > 其它 >Mondriaan‘s Dream poj-2411 鋪瓷磚 狀壓dp 寒假集訓

Mondriaan‘s Dream poj-2411 鋪瓷磚 狀壓dp 寒假集訓

技術標籤: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;
}