HDU_P1693 Eat the Trees(插头DP)

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

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))
这里写图片描述

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.

Source
2008 “Sunline Cup” National Invitational Contest

插头DP入门题,第一次敲。其实还并不怎么会,详细看注释

#include<cstdio>
#include<iostream>
using namespace std;
#define N 12
int b[N][N];
long long f[N][N][1<<N];
int main(){
    int t,n,m,cnt,cse=0;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                scanf("%d",&b[i][j]);
        memset(f,0,sizeof(f));
        cnt=(1<<(m+1));
        f[1][0][0]=1;int r1,r2;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                r1=(1<<j);r2=(1<<(j-1));
                for(int k=0;k<cnt;k++){
                    if(b[i][j]){//无障碍
                        if(((k&r1)&&(k&r2))||((~k&r1)&&(~k&r2))) //右插下插同时有或同时没有 
                            f[i][j][k]=f[i][j-1][k^r1^r2];
                        else f[i][j][k]=f[i][j-1][k]+f[i][j-1][k^r1^r2];//右插下插只有一个,可翻转可不翻转
                    }
                    else{//有障碍
                        if(!(k&r1)&&!(k&r2)) f[i][j][k]=f[i][j-1][k];//没有右插没有下插,则方案数跟随
                    }
                }
            }
            if(i+1<=N-1){//处理换行 
                for(int k=0;k<(cnt>>1);k++)
                    f[i+1][0][k<<1]=f[i][m][k];
            }
        }
        printf("Case %d: There are %I64d ways to eat the trees.\n",++cse,f[n][m][0]);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值