1. 程式人生 > >poj2411 Mondriaan's Dream【狀壓DP】

poj2411 Mondriaan's Dream【狀壓DP】

otherwise ive search long long sym body ria small for

Mondriaan‘s Dream
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 20822 Accepted: 11732

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

Ulm Local 2000

題意:

給定一個h*w的矩形。將矩形劃分成1*2的小格子,有多少種方案。

思路:

考慮用行數作為狀態,但是轉移下一行時需要上一行的劃分狀態。

所以我們多開一維用於記錄狀態。用一個整數表示。第k位是1表示第i行第k列的格子是一個豎著的1*2長方形的上半部分。

那麽對於第i+1行的狀態j, j&k=0表示沒有兩個相鄰行的相同列的格子都是長方形的上半部分。

j|k的二進制表示中,每一段連續的0都是偶數個。j|k是0的位,要麽是j和k該位都是0說明這是一個橫著的矩形。

只有這兩種情況都滿足時,(i, k)才能轉移到(i+1, j)。

最後一行要輸出的應該是沒有一個1的情況。

註意要使用long long

 1 //#include <bits/stdc++.h>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<stdio.h>
 6 #include<cstring>
 7 #include<vector>
 8 #include<map>
 9 #include<set>
10 
11 #define inf 0x3f3f3f3f
12 using namespace std;
13 typedef long long LL;
14 
15 int h, w;
16 const int maxn = 1 << 12;
17 bool in_s[maxn];
18 long long dp[12][maxn];
19 
20 int main(){
21     while(scanf("%d%d", &h, &w) != EOF && (h || w)){
22         for(int i = 0; i < 1 << w; i++){
23             bool cnt = 0, has_odd = 0;
24             for(int j = 0; j < w; j++){
25                 if(i >> j & 1) has_odd |= cnt, cnt = 0;
26                 else cnt ^= 1;
27             }
28             in_s[i] = has_odd | cnt ? 0 : 1;
29         }
30 
31         //memset(dp, 0, sizeof(dp));
32         dp[0][0] = 1;
33         for(int i = 1; i <= h; i++){
34             for(int j = 0; j < 1 << w; j++){
35                 dp[i][j] = 0;
36                 for(int k = 0; k < 1 << w; k++){
37                     if((k & j) == 0 && in_s[k | j]){
38                         dp[i][j] += dp[i - 1][k];
39                     }
40                 }
41             }
42         }
43         printf("%lld\n", dp[h][0]);
44     }
45     return 0;
46 }

poj2411 Mondriaan's Dream【狀壓DP】