zoukankan      html  css  js  c++  java
  • hdu 1693 Eat the Trees 插头dp

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



    InputThe 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.
    OutputFor 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
    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.

    最普通的吧
    https://wenku.baidu.com/view/e5314c16bcd126fff7050bf7.html?from=search
     1 #pragma GCC optimize(2)
     2 #pragma G++ optimize(2)
     3 #include<iostream>
     4 #include<algorithm>
     5 #include<cmath>
     6 #include<cstdio>
     7 #include<cstring>
     8 
     9 #define ll long long
    10 #define N 13
    11 using namespace std;
    12 inline int read()
    13 {
    14     int x=0,f=1;char ch=getchar();
    15     while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    16     while(isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
    17     return x*f;
    18 }
    19 
    20 int n,m;
    21 int p[N][N];
    22 ll f[N][N][1<<13];
    23 
    24 void solve()
    25 {
    26     memset(f,0,sizeof(f));
    27     f[0][m][0]=1;
    28     for (int i=1;i<=n;i++)
    29     {
    30         for (int j=0;j<(1<<m);j++)
    31             f[i][0][(j<<1)]=f[i-1][m][j];//十分巧妙,因为上一行末的最右边不能有插头,开头不能有插头。
    32         for (int j=1;j<=m;j++)
    33             for (int sta=0;sta<(1<<m+1);sta++)
    34             {
    35                 int x=1<<(j-1),y=1<<j;
    36                 if(p[i][j])
    37                 {
    38                     if((sta&x)&&(sta&y)) f[i][j][sta]=f[i][j-1][sta-x-y];
    39                     else if(!(sta&x) && !(sta&y)) f[i][j][sta]=f[i][j-1][sta+x+y];
    40                     else f[i][j][sta]=f[i][j-1][sta^x^y]+f[i][j-1][sta];//两个不同方向
    41                 }
    42                 else
    43                 {
    44                     if((sta&x)==0 && (sta&y)==0) f[i][j][sta]=f[i][j-1][sta];
    45                     else f[i][j][sta]=0;
    46                 }
    47             }
    48     }
    49 }
    50 int main()
    51 {
    52     int T=read(),Cas=0;
    53     while(T--)
    54     {
    55         n=read(),m=read();
    56         for (int i=1;i<=n;i++)
    57             for (int j=1;j<=m;j++)
    58                 p[i][j]=read();
    59         solve();
    60         printf("Case %d: There are %lld ways to eat the trees.
    ",++Cas,f[n][m][0]);
    61     }
    62 }
  • 相关阅读:
    Java Json 数据下划线与驼峰格式进行相互转换
    php 将数组转换网址URL参数
    Swagger2常用注解及其说明 (转)
    Java中 VO、 PO、DO、DTO、 BO、 QO、DAO、POJO的概念(转)
    bootstrap.css.map 404
    Git发生SSL certificate problem: certificate ha错误的解决方法
    防火墙禁ping:虚拟机ping不通主机,但主机可以ping虚拟机
    PhpStorm本地断点调试
    Java语言中姐种遍历List的方法总结
    Ubuntu18.04安装mysql5.7
  • 原文地址:https://www.cnblogs.com/fengzhiyuan/p/8487498.html
Copyright © 2011-2022 走看看