首页 > ACM题库 > HDU-杭电 > HDU 1693 Eat the Trees-动态规划-[解题报告] C++
2013
12-21

HDU 1693 Eat the Trees-动态规划-[解题报告] C++

Eat the Trees

问题描述 :

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

输入:

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.

输出:

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.

样例输入:

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

样例输出:

Case 1: There are 3 ways to eat the trees.
Case 2: There are 2 ways to eat the trees.

        入门插头dp,终于知道什么叫插头dp了。。。看了一下,陈丹琦的论文,然后又去sha崽哪儿逛了一圈。。。不过虽然是最简单的插头dp,要是自己写的话难度还是很大啊。。。看了一下大牛们的代码才写出来的。。。

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>

#define LL long long
#define CLR(a, b) memset(a, b, sizeof(a))

using namespace std;

const int N = 13;

LL dp[N][N][1 << N];
int g[N][N];

int main()
{
    int t, n, m, i, j, k, s, cas = 1, p, q;
    bool l, u;
    scanf("%d", &t);
    while(t --)
    {
        scanf("%d%d", &n, &m);
        for(i = 1; i <= n; i ++)
        {
            for(j = 1; j <= m; j ++)
            {
                scanf("%d", &g[i][j]);
            }
        }
        CLR(dp, 0);dp[0][m][0] = 1;
        for(i = 1; i <= n; i ++)
        {
            for(j = 0; j < (1 << m); j ++)
                dp[i][0][j << 1] = dp[i - 1][m][j];
            for(j = 1; j <= m; j ++)
                for(k = 0; k < (1 << m << 1); k ++)
                {
                    p = 1 << j;
                    q = p >> 1;
                    u = k & p;
                    l = k & q;
                    if(g[i][j])
                    {
                        dp[i][j][k] = dp[i][j - 1][k ^ p ^ q];
                        if(l != u) dp[i][j][k] += dp[i][j - 1][k];
                    }
                    else
                    {
                        if(u || l) dp[i][j][k] = 0;
                        else dp[i][j][k] = dp[i][j - 1][k];
                    }
                }
        }
        printf("Case %d: There are %I64d ways to eat the trees.\n", cas ++, dp[n][m][0]);
    }
}

解题报告转自:http://blog.csdn.net/ok_again/article/details/11579929


  1. 第2题,TCP不支持多播,多播和广播仅应用于UDP。所以B选项是不对的。第2题,TCP不支持多播,多播和广播仅应用于UDP。所以B选项是不对的。