zoukankan      html  css  js  c++  java
  • HDU-1693 Eat the Trees(插头DP)

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

    此处输入图片的描述

    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.

    偷懒只放个代码好了
    关于插头DP最好去看看cdq的论文:基于连通性状态压缩的动态规划问题

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    typedef pair<int, int> PII;
    #define inf 0x3f3f3f3f
    const ll INF = 0x3f3f3f3f3f3f3f3f;
    const ll MAXN = 1e6 + 7;
    const ll MAXM = 1e3 + 7;
    const ll MOD = 1e9 + 7;
    const double eps = 1e-6;
    int n, m, cur;
    ll dp[2][1 << 12 + 2];
    int G[15][15];
    int main()
    {
        int t;
        scanf("%d", &t);
        for (int Case = 1; Case <= t; Case++)
        {
            scanf("%d%d", &n, &m);
            for (int i = 1; i <= n; i++)
                for (int j = 1; j <= m; j++)
                    scanf("%d", &G[i][j]);
            if (n < m)
                swap(n, m);
            cur = 0;
            memset(dp[cur], 0, sizeof(dp[cur]));
            dp[cur][0] = 1;
            for (int i = 1; i <= n; i++)
            {
                for (int j = 1; j <= m; j++)
                {
                    cur ^= 1;
                    memset(dp[cur], 0, sizeof(dp[cur]));
                    for (int k = 0; k < (1 << m + 1); k++)
                    {
                        if (!G[i][j]) //障碍物 无插头 直接转移
                        {
                            if (!(k & 1 || k & (1 << m))) //障碍物有左插头或者上插头肯定不行
                                dp[cur][k << 1] += dp[cur ^ 1][k];
                        }
                        else
                        {
                            if (j != 1)
                            {
                                if (k & (1 << m) && (k & 1)) /* 有上插头和左插头*/
                                    dp[cur][(k ^ (1 << m | 1)) << 1] += dp[cur ^ 1][k];
                                else if (k & (1 << m)) /* 只有上插头 */
                                {
                                    if (j != m) /* 右插头*/
                                        dp[cur][((k ^ (1 << m)) << 1) | 1] += dp[cur ^ 1][k];
                                    if (i != n) /* 下插头 */
                                        dp[cur][((k ^ (1 << m)) | 1) << 1] += dp[cur ^ 1][k];
                                }
                                else if (k & 1) /* 只有左插头 */
                                {
                                    if (j != m) /* 右插头 */
                                        dp[cur][(k ^ 1) << 1 | 1] += dp[cur ^ 1][k];
                                    if (i != n) /*下插头 */
                                        dp[cur][k << 1] += dp[cur ^ 1][k];
                                }
                                else if (j != m && i != n) /* 没有插头只能右下插头 */
                                    dp[cur][k << 1 | 3] += dp[cur ^ 1][k];
                            }
                            else if (!(k & 1)) /* 第一位必然不可能出现左插头的情况 */
                            {
                                if (k & (1 << m)) /* 有上插头 */
                                {
                                    if (j != m) /* 右插头*/
                                        dp[cur][(k ^ (k & (1 << m))) << 1 | 1] += dp[cur ^ 1][k];
                                    if (i != n) /* 下插头 */
                                        dp[cur][(k ^ (k & (1 << m))) << 1 | 2] += dp[cur ^ 1][k];
                                }
                                else if (i != n && j != m) /* 无插头 只能接右下*/
                                    dp[cur][k << 1 | 3] += dp[cur ^ 1][k];
                            }
                        }
                    }
                }
            }
            printf("Case %d: There are %lld ways to eat the trees.
    ", Case, dp[cur][0]);
        }
        return 0;
    }
    
  • 相关阅读:
    上海电信 华为HG8240R 光猫 破解
    RedSn0w 0.9.10b5 越狱iphone 4 ios 5.0.1 Cydia闪退解决
    用IIS 7.5 Express代替IIS和开发工具vs自带的ASP.NET Development Server
    远程桌面连接问题
    Enterprise Library 5.0 Hands On Lab(1):数据访问程序块(一)
    [Havok] Havok Physics物理引擎的学习入门
    [设计模式] 深入浅出单实例Singleton设计模式(Java版)
    [C#] MD5 加密的具体流程
    [轻音乐] 理查德·克莱德曼专辑[8CD]
    [SEO] [DeDe]优化SEO
  • 原文地址:https://www.cnblogs.com/graytido/p/11180426.html
Copyright © 2011-2022 走看看