HDU

来源:互联网 发布:淘宝发布宝贝主图尺寸 编辑:程序博客网 时间:2024/06/18 13:12

Eat the Trees

 HDU - 1693 

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 2 63 – 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.

题意:求有多少种方法可以把矩阵中为1的点用回路连起来。(回路可以有多个)

我的第一个插头DP。

之前写过填多米诺骨牌的DP,没写过这种用线连接的。

其实这种类型的插头DP和之前写的那种差不多,只要弄懂了就可以写了。

https://wenku.baidu.com/view/a6dce6c76137ee06eff918d1.html(基于连通性状态压缩的动态规划问题)。

仔细看懂上面的论文应该能懂了。一定要耐心仔细的看!!!(特别是还没接触过状压DP的)

#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>using namespace std;long long d[2][1<<13];  //二维滚动数组int main(){    int T,n,m,cas=1;cin>>T;    while(T--)    {        scanf("%d%d",&n,&m);        memset(d,0,sizeof d);        int now=1;        int pre=0;        d[0][0]=1;        for(int i=0,x;i<n;i++)        {            for(int j=0;j<m;j++)            {                cin>>x;                for(int k=0;k<(1<<(m+1));k++)                {                    if(x==0)         //这个格子不能走                    {                        if((k&(1<<m))==0&&(k&1)==0)d[now][k<<1]+=d[pre][k]; //那么当上面和左边都没有插头(即连接的线)的时候,才能转移                        continue;                    }                    if((k&(1<<m))==0&&(k&1)==0&&j<m-1)  //上面和左边都没有插头且当前位置不是这一行的最后一格时,只能连接下面和右边                    {                        d[now][(k<<1)^3]+=d[pre][k];                    }                    if((k&(1<<m))&&(k&1)==0)  //上一行有插头而左一列没有的时候                    {                        d[now][(k<<1)^2^(1<<(m+1))]+=d[pre][k];     //连接上一行和下一行的格子                        if(j<m-1)d[now][(k<<1)^1^(1<<(m+1))]+=d[pre][k]; //连接上一行和右一列的格子,即L型的连接                    }                    if((k&(1<<m))==0&&(k&1))  //上一行没有插头而左一列有插头                    {                        d[now][k<<1]+=d[pre][k];    //连接左一列和下一行的格子                        if(j<m-1)d[now][(k<<1)^1^2]+=d[pre][k];  //连接左一列和右一列的格子                    }                    if((k&(1<<m))&&(1&k))    //上一行和左一列都有插头                    {                        d[now][(k<<1)^2^(1<<(m+1))]+=d[pre][k];  //这时候只能把上一行和左一列连接,不然没法形成回路                    }                    //上面有很多地方有if(j<m-1)这个条件,是因为每一行最后一列都不能有插向右边的插头                }                now^=1;                pre^=1;                memset(d[now],0,sizeof d[now]);            }        }        printf("Case %d: There are %lld ways to eat the trees.\n",cas++,d[pre][0]);    }    return 0;}