Mondriaan‘s Dream
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 17187 | Accepted: 9911 |
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
Source
1 //2017-08-02 2 #include <cstdio> 3 #include <iostream> 4 #include <cstring> 5 #include <algorithm> 6 7 using namespace std; 8 9 const int N = 15; 10 int n, m; 11 long long dp[N][1<<N];//dp[col][state]表示第col列,在state状态下(即前一列对该列的影响)的方法数。 12 13 //dfs表示当前处理到col列的第i个格子,state状态下,对下一列的影响nex 14 void dfs(int col, int i, int state, int nex){ 15 if(i == n){ 16 dp[col+1][nex] += dp[col][state]; 17 return; 18 } 19 if((state&(1<<i)) > 0)//这个格子已经被上一列填过 20 dfs(col, i+1, state, nex); 21 if((state&(1<<i)) == 0)//格子没有被填充,尝试横着放一块砖 22 dfs(col, i+1, state, nex|(1<<i)); 23 if(i+1<n && (state&(1<<i))==0 && (state&(1<<(i+1)))==0)//尝试竖着放一块砖 24 dfs(col, i+2, state, nex); 25 } 26 27 int main(){ 28 while(cin>>n>>m){ 29 if(!n && !m)break; 30 memset(dp, 0, sizeof(dp)); 31 dp[1][0] = 1; 32 for(int col = 1; col <= m; col++){ 33 for(int state = 0; state < (1<<n); state++){ 34 if(dp[col][state]) 35 dfs(col, 0, state, 0); 36 } 37 } 38 cout<<dp[m+1][0]<<endl;//答案为第m+1列,前一列对其影响为0的方法数。 39 } 40 41 return 0; 42 }