HDU 1693 Eat the Trees 插頭DP,hdu1693

來源:互聯網
上載者:User

HDU 1693 Eat the Trees 插頭DP,hdu1693

連結:http://acm.hdu.edu.cn/showproblem.php?pid=1693

題意:給出一塊r*c的地,(r,c<=11),其中有的土地上種上了樹,有些沒有種上樹,只能在種上樹的地上走,通過走若干個迴路,來走遍所有種樹的土地。問有多少種走法。

思路:無論如何還是要先提供一個連結:http://wenku.baidu.com/view/4fe4ac659b6648d7c1c74633.html,是國家隊論文,裡面對於要提及的概念講解的十分清楚。

dp的過程是從左上方的點到右下角的點進行狀態壓縮dp。dp[i][j][k]表示第i行第j列時,輪廓線上從左至右是否存在插頭的二進位狀態k(0表示輪廓線上不存在插頭,1表示輪廓線上存在插頭)。

每行第零個狀態是由右上方的狀態第c個狀態得到,具體過程是dp[i][0][j<<1]=dp[i-1][c][j]; 這是因為輪廓線從每行的第最後一種狀態變成了每行的第一種狀態(畫畫就明白了)。

狀態轉移也是根據插頭的對應情況來寫的,是由當前格子的下方輪廓線和右側輪廓線上是否存在插頭來決定。

如果下方和右側都存在插頭,那麼左側和上方就都不能存在插頭,所以狀態時由上一個狀態中對應兩位都為0的情況得到。

如果下方和右側都不存在插頭,那麼左側和上方就都必然存在插頭,所以狀態時由上一個狀態中對應兩位都為1的情況得到。

如果下方存在插頭,右側不存在插頭,或者是右側存在插頭,下方不存在插頭,那麼就可能存在上方存在插頭或者是左側存在插頭兩種情況,分別尋找對應狀態加上即可。

P.S.果然原來插頭DP的名字是基於連通性狀態壓縮的動態規劃,本質還是狀壓...

代碼:

#include <algorithm>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <ctime>#include <ctype.h>#include <iostream>#include <map>#include <queue>#include <set>#include <stack>#include <string>#include <vector>#define eps 1e-8#define INF 0x7fffffff#define maxn 13#define PI acos(-1.0)#define seed 31//131,1313//#define LOCALtypedef long long LL;typedef unsigned long long ULL;using namespace std;LL dp [maxn][maxn][1<<maxn] ;int M [maxn][maxn] ;int row,col;LL solve(int r,int c){    int tot=1<<(c+1);    memset(dp,0,sizeof(dp));    dp[0][c][0]=1;    for(int i=1; i<=r; i++)    {        for(int j=0; j<tot; j++)            dp[i][0][j<<1]=dp[i-1][c][j];        for(int j=1; j<=c; j++)        {            int st1=(1<<j),st2=(1<<(j-1));            for(int k=0; k<tot; k++)            {                if(M[i][j]==1)                {                    if((k&st1)==0&&(k&st2)==0)                        dp[i][j][k]=dp[i][j-1][k+st1+st2];                    else if((k&st1)!=0&&(k&st2)!=0)                        dp[i][j][k]=dp[i][j-1][k-st1-st2];//                    else//                    {//                        dp[i][j][k]+=dp[i][j-1][k];//                        dp[i][j][k]+=dp[i][j-1][k^st1^st2];//                    }//與下述狀態轉移等效,且更優美                    else if((k&st1)==0&&(k&st2)!=0)                    {                        dp[i][j][k]+=dp[i][j-1][k-st2+st1];                        dp[i][j][k]+=dp[i][j-1][k];                    }                    else                    {                        dp[i][j][k]+=dp[i][j-1][k-st1+st2];                        dp[i][j][k]+=dp[i][j-1][k];                    }                }                else if((k&st1)==0&&(k&st2)==0)                    dp[i][j][k]=dp[i][j-1][k];            }        }    }    return dp[r][c][0];}int main(){#ifdef LOCAL    freopen("in.txt", "r", stdin);    //freopen("out.txt", "w", stdout);#endif    int T;    scanf("%d",&T);    for(int ii=1;ii<=T;ii++)    {        scanf("%d%d",&row,&col);        for(int i=1; i<=row; i++)            for(int j=1; j<=col; j++)                scanf("%d",&M[i][j]);        printf("Case %d: There are %I64d ways to eat the trees.\n",ii,solve(row,col));    }    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.