Hdu 1693 . Eat the Trees

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liyizhixl/article/details/81591348

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))

Description

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

2
6 3
1 1 1
1 0 1
1 1 1
1 1 1
1 0 1
1 1 1
2 4
1 1 1 1
1 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.

Source

2008 “Sunline Cup” National Invitational Contest

Solution

  • 插头DP练习题!

  • f [ i ] [ j ] [ k ] 表示做完第 i 行、第 j 列、目前那 m + 1 个插头的选取二进制状态为 k 的方案数。

  • 初始值: f [ 0 ] [ m ] [ 0 ] = 1

  • 每行继承值: f [ i ] [ 0 ] [ k << 1 ] = f [ i 1 ] [ m ] [ k ]   ,   k [ 0 , 2 m )

  • 因为上一行最后一个位置不能有未闭合的插头!

  • 转移:若位置 ( i , j ) 有障碍,则看能否从前一格继承过来,不能就只能是0了。

  • 若无障碍,则肯定有转移: f [ i ] [ j ] [ k ] + = f [ i ] [ j 1 ] [ k   ^   2 j 1   ^   2 j ]

  • 如果符合条件,还能继承结果: f [ i ] [ j ] [ k ] + = f [ i ] [ j 1 ] [ k ]

  • 答案: f [ n ] [ m ] [ 0 ]

  • 时间复杂度: O ( T n m 2 m + 1 )

Code

#include<cstdio>
#include<cstring>
#include<cctype>
using namespace std;
const int N=12;
int p[N+1];
long long f[2][N][1<<N];
bool a[N][N];
inline int read()
{
    int X=0,w=0; char ch=0;
    while(!isdigit(ch)) w|=ch=='-',ch=getchar();
    while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
    return w?-X:X;
}
int main()
{
    int T=read();
    for(int t=1;t<=T;t++)
    {
        int n=read(),m=read();
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++) a[i][j]=read();
        memset(f[0],0,sizeof(f[0]));
        f[0][m][0]=1;
        int up=1<<m+1,ro=0;
        for(int i=1;i<=n;i++)
        {
            ro^=1;
            memset(f[ro],0,sizeof(f[ro]));
            for(int j=0;j<up>>1;j++) f[ro][0][j<<1]=f[ro^1][m][j];
            for(int j=1;j<=m;j++)
            {
                int x=1<<j-1,y=1<<j;
                for(int k=0;k<up;k++)
                    if(a[i][j])
                    {
                        f[ro][j][k]+=f[ro][j-1][k^x^y];
                        if(!(k&x) && !(k&y)) continue;
                        if((k&x) && (k&y)) continue;
                        f[ro][j][k]+=f[ro][j-1][k];
                    }else
                        f[ro][j][k]=(!(k&x) && !(k&y))?f[ro][j-1][k]:0;
            }
        }
        printf("Case %d: There are %lld ways to eat the trees.\n",t,f[ro][m][0]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/liyizhixl/article/details/81591348
今日推荐