1. 程式人生 > >POJ2411——Mondriaan's Dream(輪廓線dp入門)

POJ2411——Mondriaan's Dream(輪廓線dp入門)

Mondriaan's Dream
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 15207 Accepted: 8771

Description

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

題意:用1*2的磚塊去鋪滿一個n*m的矩形的方案數

思路:算是最基本的輪廓線dp的題目。磚塊只有橫著放和豎著放兩種狀態。

首先當我們試圖去填磚塊(i,j)的時候,所有磚塊(i1,j1),當其在(i,j)的左上方的時候,應該是已經被填好了的。

這樣我們從(n-1)到0和(m-1)到0區遍歷的話,就沒有後效性的問題了。

所謂的輪廓線dp,是因為dp時的狀態代表的是每一行沒有填的最上方的磚塊,一共m個。也就是相當於已經填好的磚塊的輪廓。

然後列舉輪廓線狀態去轉移就好。

<span style="font-size:14px;">#include<cstdio>
#include<cstring>
#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
using namespace std;
#define MAXN 200010
#define INF 1000000000

int d1[MAXN],d2[MAXN];
int h[MAXN];
int head[MAXN];
int p[MAXN];
set <int> s;
vector <int> ans;
struct edge{int v;int next;};
edge G[MAXN*2];
int num=0;
int vis[MAXN];
void add(int u,int v){G[num].v=v;G[num].next=head[u];head[u]=num++;};
void dfs(int u,int d[])
{
    vis[u]=true;
    for(int k=head[u];k!=-1;k=G[k].next){
        int v=G[k].v;
        if(!vis[v]){
            d[v]=d[u]+1;
            dfs(v,d);
        }
    }
}
int n,m,d;
int main(){
    scanf("%d%d%d",&n,&m,&d);
    for(int i=0;i<m;i++){
        scanf("%d",p+i);
        s.insert(p[i]);
    }
    for(int i=1;i<=n;i++){
        if(s.count(i)==0)
            ans.push_back(i);
    }
    memset(head,-1,sizeof(head));
    memset(vis,0,sizeof(vis));
    for(int i=0;i<n-1;i++){
        int a,b;
        scanf("%d%d",&a,&b);
        add(a,b);
        add(b,a);
    }
    h[1]=0;
    dfs(1,h);
    int max1=0;
    for(int i=0;i<m;i++){
        if(h[p[i]]>h[p[max1]])
            max1=i;
    }
    memset(vis,0,sizeof(vis));
    d1[p[max1]]=0;
    dfs(p[max1],d1);
    int max2=0;
    for(int i=0;i<m;i++){
        if(d1[p[i]]>d1[p[max2]])
            max2=i;
    }
    memset(vis,0,sizeof(vis));
    d2[p[max2]]=0;
    dfs(p[max2],d2);
    int cnt=0;
    for(int i=1;i<=n;i++){
        if(d1[i]<=d&&d2[i]<=d)
            cnt++;
    }
    printf("%d\n",cnt);
    return 0;
}
</span>