hdu1693 Eat the Trees

Eat the Trees

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2047 Accepted Submission(s): 995


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

hdu1693 Eat the Trees_第1张图片

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 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.
插头dp,入门!
#include <stdio.h>
#include <string.h>
using namespace std;
__int64 dp[13][13][1<<13];
int map[13][13];
int main (){
    int tcase,i,j,tt=1,n,m,k;
    scanf("%d",&tcase);
    while(tcase--){
        scanf("%d%d",&n,&m);
        for(i=0;i<n;i++)
            for(j=0;j<m;j++)
            scanf("%d",&map[i][j]);
        memset(dp,0,sizeof(dp));
        dp[1][0][0]=1;
        int state=1<<(m+1);
        for(i=1;i<=n;i++){
            for(j=0;j<m;j++){
                int k1=1<<j,k2=1<<(j+1);
                for(k=0;k<state;k++){
                    if(map[i-1][j]){
                        if((k&k1)&&(k&k2))
                        dp[i][j+1][k-k1-k2]=dp[i][j][k];//如果上左都有,那么转移后,一定就是右下为0
                        else if(!(k&k1)&&!(k&k2))
                        dp[i][j+1][k+k1+k2]=dp[i][j][k];//如果,左上都是0,转移的右下一定都是1
                        else {
                            dp[i][j+1][k]+=dp[i][j][k];//两种方式走
                            dp[i][j+1][k^k1^k2]+=dp[i][j][k];
                        }
                    }
                    else {
                        if(!(k&k1)&&!(k&k2))
                        dp[i][j+1][k]=dp[i][j][k];//如果都是0,才可以转移!
                    }
                }
            }
            for(k=0;k<state;k++)//轮廓线最右边的移到最左边
            dp[i+1][0][k<<1]=dp[i][m][k];
        }
        printf("Case %d: There are %I64d ways to eat the trees.\n",tt++,dp[n][m][0]);
    }
    return 0;
}

你可能感兴趣的:(hdu1693 Eat the Trees)