POJ

来源:互联网 发布:软件汉化 编辑:程序博客网 时间:2024/06/09 14:43

Mondriaan's Dream

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 21 31 42 22 32 42 114 110 0

Sample Output

10123514451205

Source

Ulm Local 2000




题意:给你一堆1*2的骨牌,问你铺满h*w的棋盘有多少种铺法。


解题思路:状压DP,做完这道题,对状态压缩有更深的理解了。我们用dp[i][j]代表前i个行已经铺好了,且第i行的状态为j的方法数。如11011就代表该行的第1245个格子有骨牌。然后我们就可以枚举所有状态,如果当前状态能够有一个方法使其变为11111111....,那么就状态转移。这个铺的过程用深度优先搜索枚举铺的方法。这道题启发了我状压DP肯定是要遍历所有状态的……详细看代码。




#include<iostream>#include<deque>#include<memory.h>#include<stdio.h>#include<map>#include<string>#include<algorithm>#include<vector>#include<math.h>#include<stack>#include<queue>#include<set>#define INF 1<<29using namespace std;typedef long long int ll;ll dp[15][1<<12];int h,w;//当前行的初始状态,当前行现在的状态,下一行的状态,现在是第几列,现在是第几行void dfs(int prestate,int nowstate,int nextstate,int n,int cnt){    //如果该行被填满了    if(n>=w){        dp[cnt+1][nextstate]+=dp[cnt][prestate];//状态转移        return;    }    //如果该列已经有骨牌,跳到下一列    if(((1<<n)&nowstate)!=0){        dfs(prestate,nowstate,nextstate,n+1,cnt);        return;    }    //如果该列没有骨牌,那么往该列竖着插一个骨牌    dfs(prestate,nowstate|(1<<n),nextstate|(1<<n),n+1,cnt);    //如果有两个空位    if(n+1<w&&((1<<(n+1))&nowstate)==0){        //往这两个空位横着放一个骨牌,只影响当前行的状态,不影响下一行        dfs(prestate,nowstate|(1<<n)|(1<<(n+1)),nextstate,n+2,cnt);    }}int main(){    while(~scanf("%d%d",&h,&w)){        if(h==0&&w==0)            break;        memset(dp,0,sizeof(dp));                dp[1][0]=1;//空的铺法只有一种,就是空。        int state=1<<w;        for(int i=1;i<=h;i++)//一行一行的递推            for(int j=0;j<state;j++)//遍历所有状态                dfs(j,j,0,0,i);        printf("%lld\n",dp[h+1][0]);    }    return 0;}










原创粉丝点击