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
Sample Output
题意
给出一个 n*m
的方格,问用 1*2
的小方格来填充总共有多少种方法。
思路
我们定义 dp[i][j]
代表第 i-1
行已经放满,第 i
行状态为 j
时候的方案数。
其中每一行的状态我们可以用一个二进制来表示, 0
代表未填充, 1
代表已填充。
因为方块有两种摆放形式:竖放、横放
所以当第 i
行第 j
列竖放一个方块时,第 i-1
行第 j
列需要留空;而当第 i
行第 j
列与第 j+1
列横放一个方块时,第 i-1
行第 j
列与第 j+1
列则需已填充,因为我们定义的 dp
需要把第 i-1
行全部放满。
状态转移方程: dp[i][s]=sum(dp[i−1][si]) 其中状态 s
与状态 si
必须兼容,也就是状态 s
中竖放的块能够填满状态 si
中的空。
这里有一个优化,也就是当 n∗m
AC 代码