首页 > 题解 > poj 2411 Mondriaan’s Dream

poj 2411 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.

images
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.

images

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

题意

用12和21的小块覆盖矩阵。。。

题解

1, 因为小的矩形为12或21,那么由这两种构成的大矩形的面积必然为偶数,所以我们可以知道若是n*m为奇数,那么答案必然为0.

2, 那么我们如今来解析小矩形的摆放景象: ①横着放   ②和上方一行一路竖着放  ③和下面一路竖着放。

a:

1   1   来默示横放了一个矩形;
b:

0

1     来默示和上方一行一路竖着放了一个矩形;

c:

1

0     默示和下面一行一路放了一个矩形;

总结 :至于为什么这么放呢,因为如许 才干够区分出怎么放的,你可以动手画画,在所有画出来的图中,只有以上三种矩形的摆放形式!

3, 那么按照矩形的摆放的形式,我们知道当前行只会受到上方一行的影响。那么我们用dp[i][j]默示前i-1行已经摆放好的景象下第i行的状况为j时的摆放数,那么我们知道dp[i][j] += dp[i-1][k] (k是i-1所有满足的状况)。如今我们就是去解析每一行的状况了,我们利用二进制的思惟,因为有m 列,那么每一行的最多的状况数为(1<<m)-1个,所以我们就可以经由过程列举去求出每一行的满足的前提,最后的答案就是dp[n-1][(1<<m)-1](n是从0开始的).

4, 如今推敲第i行j列这个dp[i][j]式子:(有点类似枚举的意思)

① 若是是横放矩形,那么就是这一行和上方一行的i和j都是1  ;(可能你会有疑问,为啥不可能是竖着摆放,那就再好好想想那三种摆放形式,当前状况已是横放,所以那两种竖放的小矩形形式不可能再出现了,仔细详细)

② 若是是竖放有两种景象:若是是和上一行竖放那么这一行的j列为1,上一行的j列为0 ; 若是是和下一行竖放的,那么就是这一行的j列为0,下一行必然为要为1才能满足要求;

5, 初始化第一行,这特殊的一行,因为第一行是没有前面一行的。所以必须先预处理出第一行满足的所有的状况,然后列举 1 ~ n-1 行。

6, 这里求出每一行的满足的状况时有两种思路:①是经由过程dfs的思路,直接搜出所有的可能满足的状况  ②直接暴力列举1-(1<<m)-1所有的状况。

第二种写法

来自discuss

images


如果你觉的这篇文章不错,分享给朋友吧!

打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮

×