sgu 131 - Hardwood floor

來源:互聯網
上載者:User

標籤:io   ar   for   sp   問題   on   amp   bs   as   

題目:在m*n的地板上鋪上相同的I型和L型的地板磚,問有多少種鋪法。

分析:dp,組合,計數。經典dp問題,狀態壓縮。與zoj1100一樣,只是多了幾個狀態。

            狀態:設f(i,j)為前i-1行鋪滿,第i行鋪的狀態的位表示為j時的鋪磚種類數;

            轉移:I型的磚,因為只能橫鋪或者豎鋪,那麼一個磚塊鋪之前的狀態只有兩種;

                      且如果當前豎放會對下一行產生影響,建立相鄰兩行狀態對應關係;

                      L型的磚,因為一定會影響兩層,共有4中放法,建立相鄰曾關係;

                      這裡利用dfs找到所有f(i,j)的上一行的所有前置狀態f(i-1,k)加和即可;

                      f(i,j)= sum(f(i-1,k)){ 其中,f(i-1,k)可以產生f(i,j)狀態 };

           (本來準備儲存狀態的,不過會 MLE,所以最後一邊擴充,一邊加了。)

說明:只注意用%i64d,和zoj正好相反。(2011-09-28 15:47)。

#include <stdio.h>#include <stdlib.h>#include <string.h>long long F[ 10 ][ 1<<9 ];//用dfs找到可以到達本狀態的上層的狀態 void dfs( int A, int B, int C, int D ){    if ( !A ) {        F[ C ][ D ] += F[ C-1 ][ B ];        return;    }else {        int V = A&-A;//取得最後一個 1的位置         if ( B&V ) {            dfs( A&~V, B&~V, C, D ); //豎著放 1*2             if ( V>1 && (B&(V>>1)) ) dfs( A&~V, B&~(3*(V>>1)), C, D );            if ( B&(V<<1) ) dfs( A&~V, B&~(3*V), C, D );        }         if ( A&(V<<1) ) {            dfs( A&~(3*V), B, C, D );            if ( B&V ) dfs( A&~(3*V), B&~V, C, D );            if ( B&(V<<1) ) dfs( A&~(3*V), B&~(V<<1), C, D );        }    }}int main(){    int n,m;    while ( scanf("%d%d",&n,&m) != EOF && m ) {                if ( m>n ) {int t = m;m = n;n = t;}                int M = (1<<m)-1;                for ( int i = 0 ; i <= n ; ++ i )        for ( int j = 0 ; j <= M ; ++ j )            F[ i ][ j ] = 0LL;        F[ 0 ][ M ] = 1LL;                for ( int i = 1 ; i <= n ; ++ i )        for ( int j = 0 ; j <= M ; ++ j )            dfs( j, M, i, j );                printf("%I64d\n",F[ n ][ M ]);    }    return 0;}

sgu 131 - Hardwood floor

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.