HDU 1693 Eat the Trees(轮廓线DP)

来源:互联网 发布:音频裁切软件 编辑:程序博客网 时间:2024/05/22 17:36



Problem Description
Most of us know that in the game called DotA(Defense of the Ancient), Pudge is a strong hero in the first period of the game. When the game goes to end however, Pudge is not a strong hero any more.
So Pudge’s teammates give him a new assignment—Eat the Trees!

The trees are in a rectangle N * M cells in size and each of the cells either has exactly one tree or has nothing at all. And what Pudge needs to do is to eat all trees that are in the cells.
There are several rules Pudge must follow:
I. Pudge must eat the trees by choosing a circuit and he then will eat all trees that are in the chosen circuit.
II. The cell that does not contain a tree is unreachable, e.g. each of the cells that is through the circuit which Pudge chooses must contain a tree and when the circuit is chosen, the trees which are in the cells on the circuit will disappear.
III. Pudge may choose one or more circuits to eat the trees.

Now Pudge has a question, how many ways are there to eat the trees?
At the picture below three samples are given for N = 6 and M = 3(gray square means no trees in the cell, and the bold black line means the chosen circuit(s))


 

Input
The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases.
For each case, the first line contains the integer numbers N and M, 1<=N, M<=11. Each of the next N lines contains M numbers (either 0 or 1) separated by a space. Number 0 means a cell which has no trees and number 1 means a cell that has exactly one tree.
 

Output
For each case, you should print the desired number of ways in one line. It is guaranteed, that it does not exceed 263 – 1. Use the format in the sample.
 

Sample Input
26 31 1 11 0 11 1 11 1 11 0 11 1 12 41 1 1 11 1 1 1
 

Sample Output
Case 1: There are 3 ways to eat the trees.Case 2: There are 2 ways to eat the trees.
 
题意:N*M地图,能组成多少不同的环,可以有多个环在图上。

分析:轮廓线DP,  可见2008年陈丹琦的大作——《基于连通性状态压缩的动态规划问题》,上面对于插头、轮廓线的概念有详细的解释。

代码:

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <stack>#include <map>#include <set>#include <vector>#include <queue>#define mem(p,k) memset(p,k,sizeof(p));#define rep(a,b,c) for(int a=b;a<c;a++)#define pb push_back#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define ll long long#define inf 0x3f3f3f3fusing namespace std;const int maxn=10;int n,m,ans;ll mapp[12][12],dp[2][1<<13];int main(){    int t,cur=1;    cin>>t;    while(t--){        scanf("%d%d",&n,&m);        for(int i=0;i<n;i++){            for(int j=0;j<m;j++)scanf("%lld",mapp[i]+j);        }        mem(dp,0);        int now=0;        dp[now][0]=1;        for(int i=0;i<n;i++){            for(int j=0;j<m;j++){                now^=1;                mem(dp[now],0);                for(int k=0;k<2<<m;k++){                    if(!mapp[i][j]){//unable                        if((k&3<<j)==0){                            if(j<m-1)dp[now][k]+=dp[now^1][k];                            else dp[now][k<<1]+=dp[now^1][k];                        }                    }                    else{//able                        if(j<m-1){                            if((k&3<<j)==0){                                dp[now][k|3<<j]+=dp[now^1][k];                            }                            else if((k>>j&3)<3){                                dp[now][k]+=dp[now^1][k];                                dp[now][k^3<<j]+=dp[now^1][k];                            }                            else{                                dp[now][k^3<<j]+=dp[now^1][k];                            }                        }                        else{                            if((k>>j&3)<3 && (k>>j&3)){                                dp[now][((k|3<<j)^2<<j)<<1]+=dp[now^1][k];                            }                            else if((k>>j&3)==3){                                dp[now][(k^3<<j)<<1]+=dp[now^1][k];                            }                        }                    }                    //cout<<now<<" "<<k<<"= "<<dp[now][k]<<endl;                }            }        }        cout<<"Case "<<cur++<<": There are "<<dp[now][0]<<" ways to eat the trees."<<endl;    }    return 0;}