HDU 1693 Eat the Trees 插头dp

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1693

Eat the Trees

Time Limit: 4000/2000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
#### 问题描述 > 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)) #### 输入 > 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. #### 输出 > 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. ####样例输入 > 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

样例输出

Case 1: There are 3 ways to eat the trees.
Case 2: There are 2 ways to eat the trees.

题意

求不经过障碍物的哈密顿回路有多少种,可以独立的几个哈密顿回路。

题解

插头dp逐格转移。
对于每个格子:
1、上左都没有插头 --右下有插头
2、只有上方有插头 --上右有插头或上下有插头
3、只有左部有插头 --左下有插头或左右有插头
4、上左部都有插头 --上左有插头

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf

typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;

const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-9;

const double PI = acos(-1.0);

//start----------------------------------------------------------------------

const int maxn=13;
LL dp[2][1<<maxn];
int n,m;
int pre,cur;

int main() {
    int tc,kase=0;
    scf("%d",&tc);
    while(tc--) {
        scf("%d%d",&n,&m);

        ///滚动数组
        pre=0,cur=1;
        clr(dp[cur],0);
        dp[cur][0]=1;

        ///逐格转移
        for(int i=0; i<n; i++) {
            for(int j=0; j<m; j++) {
                int x;
                scf("%d",&x);

                swap(pre,cur);
                clr(dp[cur],0);

                ///p1为当前插头的左侧,p2为当前插头的上侧
                int p1=(1<<j),p2=(1<<(j+1));

                ///根据具体情况吧左侧转移到下侧,上侧转移的右侧
                for(int k0=0; k0<(1<<(m+1)); k0++) {
                    int k=k0;

                    ///换行的时候需要位移一位,轮廓线画一画就会有感觉
                    if(!j) k=k0<<1;

                    ///没有树的情况
                    if(!x) {
                        ///必须都没有插头才合法
                        if(!(p1&k)&&!(p2&k)) dp[cur][k]+=dp[pre][k0];
                        continue;
                    }

                    ///状压维护的是轮廓线上是否有插头的状态
                    if(!(p1&k)&&!(p2&k)) {
                        ///右下侧有插头
                        if(i<n-1&&j<m-1) dp[cur][k^p1^p2]+=dp[pre][k0];
                    } else if(!(p1&k)&&(p2&k)) {
                        ///上下侧有插头
                        if(i<n-1) dp[cur][k^p1^p2]+=dp[pre][k0];
                        ///上右侧有插头
                        if(j<m-1) dp[cur][k]+=dp[pre][k0];
                    } else if((p1&k)&&!(p2&k)) {
                        ///左右侧有插头
                        if(j<m-1) dp[cur][k^p1^p2]+=dp[pre][k0];
                        ///左下侧有插头
                        if(i<n-1) dp[cur][k]+=dp[pre][k0];
                    } else {
                        ///左上侧有插头
                        dp[cur][k^p1^p2]+=dp[pre][k0];
                    }
                }
            }
        }
        prf("Case %d: There are %lld ways to eat the trees.\n",++kase,dp[cur][0]);
    }
    return 0;
}

//end-----------------------------------------------------------------------
posted @ 2016-10-25 14:44  fenicnn  阅读(147)  评论(0编辑  收藏  举报