遞推+狀態壓縮+動態規劃-POJ-Mondriaan’s Dream

來源:互聯網
上載者:User

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

1<=h,w<=11. 資料限制較小,可以用狀態壓縮方法。

首先 我們先求用1*2 的矩形拼成 n*m的矩形有多少種拼法  

分兩個步驟, 1) 先求出相鄰兩行的轉化關係

             2) 通過相鄰兩行的轉化關係算出經過n次轉化有幾種方法能拼成n*m的矩陣

在別人的基礎上再做些解釋:

1) 狀態標記 橫放佔兩位都為1,豎放佔一位為1(上1下0),不放置佔兩位都為0 ,每行可以轉化為1個2進位數。

當該行放完時,即隊應 11111 (m個), 即2^m-1, dp[n][2^m-1] 即最終結果。


豎放的只所以佔一位,是由於我們是從左下角往右上方遞推,此處可以豎放說明它的前一行此處必然沒放置,即為0.

此行的計算,只需由前一行遞推可得。因此可用滾動數組節省一點記憶體(非重點)。


 //根據橫放和豎放,可以把每行根據01合成一個位元,其實就是狀態壓縮。

對於每一個位置,我們有三种放置方法:

1.       豎直放置

2.       水平放置

3.       不放置

d為當前列號 ,初始化d, s1, s2都為0;對應以上三种放置方法,s1, s2的調整為: s1 為當前行, s2為當前行的上一行

1. d = d + 1, s1 << 1 | 1, s2 << 1;            //   豎直放置   當前行為1,上一行為0

2. d = d + 2, s1 << 2 | 3, s2 << 2 | 3;      //    橫放 都為11(11=3)

3. d = d + 1, s1 << 1,   s2 << 1 | 1;         //     上一行為1,不能豎放,不放置的狀態(豎放為出現不能放置的情況,橫放可以一直向右拓展,越界則不計算即可)

  

// s1<<1 | 1 意思是,先把s1左移1位,然後位元運算或上 1的位元,即 01 。

另外:第一行單獨計算

//============================================================================// Name        : POJ2411.cpp// Author      : // Version     :// Copyright   : Your copyright notice// Description : Hello World in C++, Ansi-style//============================================================================#include <iostream>#include <stdio.h>#include <string.h>using namespace std;int const MAXN = (1 << 11);__int64 dp[11][4100];int m,n,row;void dfs(int c,int s,int ps){if(c > m) return;if(c == m){dp[row][s] += dp[row-1][ps];return;}dfs(c+1, s<<1, (ps << 1) | 1); //不放dfs(c+1, (s<<1) | 1, ps << 1); //豎放dfs(c+2, (s<<2) | 3, ps << 2 | 3); //橫放}void dfs_first(int c,int s){if(c > m) return;if(c == m){dp[0][s] = 1;}dfs_first(c+1, s<<1); //不放dfs_first(c+2, (s<<2) | 3); //橫放}int main() {freopen("in.txt", "r", stdin);int i;while(scanf("%d %d", &m, &n),m){if(m*n % 2){puts("0"); continue;}if(m < n) swap(m,n);memset(dp, 0, sizeof(dp));dfs_first(0,0);for(i=1; i<n; i++){row = i;dfs(0,0,0);}printf("%I64d\n", dp[n-1][(1<<m) -1]);}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.