zoukankan      html  css  js  c++  java
  • HDU1693 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)) 

    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 �C 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

    累得很,想着快点A了去睡觉,结果真的没怎么调就A了

    果然休息是人最大的动力吗233

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<cstring>
     5 #define LL long long
     6 using namespace std;
     7 const int mxn=15;
     8 int read(){
     9     int x=0,f=1;char ch=getchar();
    10     while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    11     while(ch>='0' && ch<='9'){x=x*10-'0'+ch;ch=getchar();}
    12     return x*f;
    13 }
    14 int mp[mxn][mxn];
    15 LL f[2][1<<13];
    16 LL tmp[1<<13];
    17 int n,m;
    18 int main(){
    19     int i,j,cas=0;
    20     int T=read();
    21     while(T--){
    22         memset(f,0,sizeof f);
    23         n=read();m=read();
    24         for(i=1;i<=n;i++)
    25             for(j=1;j<=m;j++)
    26                 mp[i][j]=read();
    27         int ed=(1<<(m+1));
    28         int now=0,pre=1;
    29         f[now][0]=1;
    30         for(i=1;i<=n;i++){
    31             for(j=ed-1;j>=0;j--)tmp[j]=f[now][j];
    32             memset(f[now],0,sizeof f[now]);
    33             for(j=ed-1;j>=0;j--)f[now][j<<1]=tmp[j];
    34 //            for(j=0;j<(ed>>1);j++)f[now][j<<1]=f[now][j];
    35             for(j=1;j<=m;j++){
    36                 swap(now,pre);
    37                 memset(f[now],0,sizeof f[now]);
    38                 for(int k=0;k<ed;k++){
    39 //                    printf("k:%d w:%I64d  ",k,f[pre][k]);
    40                     int x=k&(1<<(j-1));
    41                     int y=k&(1<<j);
    42                     if(mp[i][j]){
    43                         if(x && y){//11 -> 00
    44                             f[now][k^(1<<(j-1))^(1<<j)]+=f[pre][k];
    45                         }
    46                         else if(x && !y){//10 -> 01  10
    47                             f[now][k]+=f[pre][k];
    48                             f[now][k^(1<<(j-1))^(1<<j)]+=f[pre][k];
    49                         }
    50                         else if(!x && y){//01 -> 01  10
    51                             f[now][k]+=f[pre][k];
    52                             f[now][k^(1<<(j-1))^(1<<j)]+=f[pre][k];
    53                         }
    54                         else if(!x && !y){//00 -> 11
    55                             f[now][k^(1<<(j-1))^(1<<j)]+=f[pre][k];
    56                         }
    57                     }
    58                     else{
    59                         if(!x && !y)f[now][k]=f[pre][k];
    60                     }
    61                 }
    62 //                printf("%d %d:%I64d
    ",i,j,f[now][0]);
    63             }
    64         }
    65         printf("Case %d: There are %I64d ways to eat the trees.
    ",++cas,f[now][0]);
    66     }
    67     return 0;
    68 }
  • 相关阅读:
    __file__ 作用以及模块导入方法
    random 模块
    time 模块
    module模块和包
    装饰器
    python闭包
    filter map reduce函数的使用
    python文件处理
    python内置函数使用
    函数
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/6442411.html
Copyright © 2011-2022 走看看