hdu1693 Eat the Trees 插头DP

来源:互联网 发布:淘宝客服术语模板 编辑:程序博客网 时间:2024/05/10 15:26

Eat the Trees

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3089    Accepted Submission(s): 1491


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.

  用回路把树包围有多少种情况。实际上就是所有路径都是回路,不经过树,回路之间不能交叉的情况有多少种。

  用M+1位表示轮廓线状态,dp[i][j][s]表示第i行第j列对应的状态是s有多少种。

  不是树的格子必须要有两个插头,画个图就比较清楚了。

    是树的格子不能有插头。

    注意每行转移的时候是把上一行最后一列的状态左移一位。

    把dp[0][M][0]初始化为1,最后答案是dp[N][M][0]。

    #include<iostream>    #include<cstdio>    #include<string>    #include<cstring>    #include<vector>    #include<cmath>    #include<queue>    #include<stack>    #include<map>    #include<set>    #include<algorithm>    #define INF 0x3f3f3f3f    using namespace std;    typedef long long LL;    const int MAXN=13;    const int MAXM=20010;    int T;    int N,M;    int a[MAXN][MAXN];    LL dp[MAXN][MAXN][1<<13];    int main(){        freopen("in.txt","r",stdin);        scanf("%d",&T);        int cas=0;        while(T--){            scanf("%d%d",&N,&M);            for(int i=1;i<=N;i++)                for(int j=1;j<=M;j++) scanf("%d",&a[i][j]);            memset(dp,0,sizeof(dp));            dp[0][M][0]=1;            for(int i=1;i<=N;i++){                for(int s=0;s<(1<<M);s++) dp[i][0][s]=dp[i-1][M][s<<1];                for(int j=1;j<=M;j++){                    for(int s=0;s<(1<<(M+1));s++){                        int p=1<<(M-j),q=(1<<(M-j+1));                        if(a[i][j]==1){                            if((s&p)&&(s&q)||!(s&p)&&!(s&q)) dp[i][j][s]=dp[i][j-1][s^p^q];                            else dp[i][j][s]=dp[i][j-1][s]+dp[i][j-1][s^p^q];                        }                        else{                            if(!(s&p)&&!(s&q)) dp[i][j][s]=dp[i][j-1][s];                            else dp[i][j][s]=0;                        }                    }                }            }            printf("Case %d: There are %I64d ways to eat the trees.\n",++cas,dp[N][M][0]);        }        return 0;    }





0 0
原创粉丝点击