POJ 2411-Mondriaan's Dream(状压DP->骨牌铺满问题)

来源:互联网 发布:素颜霜 知乎 编辑:程序博客网 时间:2024/05/16 17:11
Mondriaan's Dream
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 17226 Accepted: 9935

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

[Submit]   [Go Back]   [Status]   [Discuss]

Home Page   Go Back  To top

题意:给你一个n*m的矩形,让你用1*2的矩形覆盖它,使得该矩形被铺满,问你有多少种方案。

题解:这是一个经典问题,记得去年省赛好像出了,但是当时是一脸懵逼。。。。。听说2*n的矩形覆盖的方案数是一个公式。。感觉6到飞起,然而这道题是n*m的矩形,呢就只能老老实实状压DP了,但其实这道题的姿势好像有很多。。。什么轮廓线DP,插头DP,dfs什么的。。。然而我都不会,就只能状压随便搞搞了,然而具体要怎么搞呢,问题还是如何将当前已经放好的骨牌转化成2进制的形式,我们定义DP[i][j]表示前i-1行已经铺好的情况下第i行状态为j的方案数。。。然后就是状态转移了。。怎么转移呢??仔细想想都有那些情况。

首先假如当前行第j列的骨牌是竖着放的,呢要保证不与之前已经放好的骨牌冲突的情况下,第i-1行第j列必须是空着的,也就是该位是0,不然一定会冲突的是吧,故假如第i-1行第j列是空着的,呢此时第i行第j列就已经确定,且是唯一确定的!齐次就是横着放的情况,这种情况是不影响第i-1行的,只需判定当前状态第i+1位是否为1即可。。。。其他就是自己与自己冲突的情况了,代码里有注释。。。。

#include<map>      #include<stack>      #include<queue>      #include<vector>      #include<math.h>      #include<stdio.h>      #include<iostream>      #include<string.h>      #include<stdlib.h>      #include<algorithm>      using namespace std;      typedef long long  ll;      #define inf 1000000000     #define mod 100000000       #define  maxn  1<<13    #define  lowbit(x) (x&-x)      #define  eps 1e-10  int n,m;ll dp[15][maxn];bool check(int x){for(int i=0;i<m;){if(x&(1<<i)){if(i==m-1)return 0;if(x&(1<<(i+1)))i+=2;elsereturn 0;}elsei++;}return 1;}bool ok(int x,int y){for(int i=0;i<m;){if(x&(1<<i)){if(y&(1<<i))//第i-1行的第j列也为1,则第i行必然是横放{//若第i行和第i-1行的j+1列不是1,则非法if(i==m-1 || !(x&(1<<(1+i))) || !(y&(1<<(1+i))))return 0;elsei+=2;}else//第i-1行第j列为0,则说明第i行第j列是竖放的i++;}else//第i行第j列为0,则第i-1行的第j列必须是已经填充了的。{if(y&(1<<i))//若已经填充i++;elsereturn 0;}}return 1;}void work(){int i,j,k;if(n<m)swap(n,m);//让每一行的状态数尽可能少memset(dp,0,sizeof(dp));for(i=0;i<(1<<m);i++)if(check(i))dp[1][i]=1;for(i=2;i<=n;i++)for(j=0;j<(1<<m);j++)for(k=0;k<(1<<m);k++)if(ok(j,k))dp[i][j]+=dp[i-1][k];printf("%lld\n",dp[n][(1<<m)-1]);}int main(void){while(scanf("%d%d",&n,&m)!=EOF){if(n== 0 && m==0)break;if(n*m%2!=0){printf("0\n");continue;}work();}return 0;}


原创粉丝点击