题目链接:https://vjudge.net/problem/HDU-1693

Eat the Trees

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

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
 
Recommend
wangye

题意:

用一个或者多个回路去经过所有空格子,问总共有多少种情况?

题解:

有关路径的插头DP。

1.由于只需要记录轮廓线上m+1个位置的插头情况(0或1),且m<=11,2^11 = 2048,故可以用二进制对轮廓线的信息进行压缩。

2.接着就是人工枚举了。

对于如何维护轮廓线的理解:

1.对于棋盘。我们需要将其开始下标设为1(方便维护轮廓线)。

2.将要处理第i行第1列的时候,它的左插头的信息记录在第0位,它的上插头的信息刚好记录在第1位。

3.当处理完第i行第1列的时候,我们就把它的左插头改为下插头,上插头改为右插头。这样,对于第i行第2列来说,它的左插头记录在第1位,上插头记录在第2位。所以可以总结出:对于当前要转移的格子a[i][j],它的左插头的信息记录在第j-1位,它的上插头记录在第j位。

4.当转移完第i行最后一个格子的时候,左插头就位于轮廓线的最后位置,即其信息记录在最高位。而我们在转到下一行第一列的时候,由于a[i+1][1]的左插头是记录在第0位的,所以就需要把最高位的移到第0位,后面的位又往后移动一位,即左移。我们又知道:第一列的格子是没有左插头的,所以,在转行的时候,有效的轮廓线是没有左插头轮廓线,我们取这些有效的轮廓线,并对其压缩信息左移一位即可。

写法一:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e5; int n, m, Map[][];
LL dp[][][<<]; int main()
{
int T, kase = ;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &n, &m);
for(int i = ; i<=n; i++)
for(int j = ; j<=m; j++)
scanf("%d", &Map[i][j]); memset(dp, , sizeof(dp));
dp[][m][] = ; //初始状态
for(int i = ; i<=n; i++)
{
//可知转移完行末的格子后,左插头在轮廓线上最后一个位置,即其信息记录在最后一个位
//由于第一列的格子肯定没有左插头,所以只需枚举到(1<<m)-1,以去除掉含有左插头的情况。
for(int s = ; s<(<<m); s++)
dp[i][][s<<] = dp[i-][m][s]; for(int j = ; j<=m; j++)
{
for(int s = ; s<(<<m+); s++) //枚举上一个格子的所以状态,即当前格子的轮廓线
{
int up = <<j; //上插头
int left = <<(j-); //左插头
bool have_up = s&up;
bool have_left = s&left; if(!Map[i][j]) //当前格子不可行
{
if( !have_up && !have_left ) //当没有插头时,才能转移
dp[i][j][s] += dp[i][j-][s];
}
else //当前格子可行
{
if( !have_up && !have_left ) //两个格子都不存在,新建分量
{
dp[i][j][s^up^left] += dp[i][j-][s];
}
else if( have_up && !have_left ) //有上插头无左插头
{
dp[i][j][s] += dp[i][j-][s]; //往右延伸
dp[i][j][s^up^left] += dp[i][j-][s]; //往下延伸
}
else if( !have_up && have_left ) //无上插头有左插头
{
dp[i][j][s] += dp[i][j-][s]; //往下延伸
dp[i][j][s^up^left] += dp[i][j-][s]; //往右延伸
}
else //两个插头都存在,只能合并分量
{
dp[i][j][s^up^left] += dp[i][j-][s];
}
}
}
}
}
printf("Case %d: There are %lld ways to eat the trees.\n", ++kase, dp[n][m][]);
}
}

写法二(写法一的简写):

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e5; int n, m, Map[][];
LL dp[][][<<]; int main()
{
int T, kase = ;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &n, &m);
for(int i = ; i<=n; i++)
for(int j = ; j<=m; j++)
scanf("%d", &Map[i][j]); memset(dp, , sizeof(dp));
dp[][m][] = ; //初始状态
for(int i = ; i<=n; i++)
{
//可知转移完行末的格子后,左插头在轮廓线上最后一个位置,即其信息记录在最后一个位
//由于第一列的格子肯定没有左插头,所以只需枚举到(1<<m)-1,以去除掉含有左插头的情况。
for(int s = ; s<(<<m); s++)
dp[i][][s<<] = dp[i-][m][s]; for(int j = ; j<=m; j++)
{
for(int s = ; s<(<<m+); s++) //枚举上一个格子的所以状态,即当前格子的轮廓线
{
int up = <<j; //上插头
int left = <<(j-); //左插头
bool have_up = s&up;
bool have_left = s&left; if(!Map[i][j]) //当前格子不可行
{
if( !have_up && !have_left ) //当没有插头时,才能转移
dp[i][j][s] += dp[i][j-][s];
}
else //当前格子可行
{
dp[i][j][s^up^left] += dp[i][j-][s];
if( (have_up&&!have_left) || (!have_up&&have_left) )
dp[i][j][s] += dp[i][j-][s];
}
}
}
}
printf("Case %d: There are %lld ways to eat the trees.\n", ++kase, dp[n][m][]);
}
}

对写法一用哈希表优化了一下,但是过不了,差不出错误。先放着:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e5;
const int HASH = 1e4; int n, m, Map[][]; struct
{
int size, head[HASH], next[MAXN];
int state[MAXN];
LL sum[MAXN]; void init()
{
size = ;
memset(head, -, sizeof(head));
} void insert(int status, LL Sum)
{
int u = status%HASH;
for(int i = head[u]; i!=-; i = next[i])
{
if(state[i]==status)
{
sum[i] += Sum;
return;
}
}
state[size] = status; //头插法
sum[size] = Sum;
next[size] = head[u];
head[u] = size++;
} }Hash_map[]; void transfer(int i, int j, int cur)
{
for(int k = ; k<Hash_map[cur].size; k++)
{
int s = Hash_map[cur].state[k];
LL Sum = Hash_map[cur].sum[k]; int up = <<j;
int left = <<(j-);
bool have_up = s&up;
bool have_left = s&left; if(!Map[i][j])
{
Hash_map[cur^].insert(s<<(j==m), Sum);
}
else
{
if( !have_up && !have_left )
{
if(Map[i+][j] && Map[i][j+])
Hash_map[cur^].insert((s^up^left)<<(j==m), Sum);
}
else if( have_up && !have_left ) //有上插头无左插头
{
if(Map[i][j+])
Hash_map[cur^].insert(s<<(j==m), Sum);
if(Map[i+][j])
Hash_map[cur^].insert((s^up^left)<<(j==m), Sum);
}
else if( !have_up && have_left ) //无上插头有左插头
{
if(Map[i][j+])
Hash_map[cur^].insert((s^up^left)<<(j==m), Sum);
if(Map[i+][j])
Hash_map[cur^].insert(s<<(j==m), Sum);
}
else
{
Hash_map[cur^].insert((s^up^left)<<(j==m), Sum);
}
}
}
} int main()
{
int T, kase = ;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &n, &m);
memset(Map, , sizeof(Map));
for(int i = ; i<=n; i++)
for(int j = ; j<=m; j++)
scanf("%d", &Map[i][j]); int cur = ;
Hash_map[cur].init();
Hash_map[cur].insert(, );
for(int i = ; i<=n; i++)
{
for(int j = ; j<=m; j++)
{
Hash_map[cur^].init();
transfer(i, j, cur);
cur ^= ;
}
}
printf("Case %d: There are %lld ways to eat the trees.\n", ++kase, Hash_map[cur].sum[]);
}
}

HDU1693 Eat the Trees —— 插头DP的更多相关文章

  1. HDU1693 Eat the Trees 插头dp

    原文链接http://www.cnblogs.com/zhouzhendong/p/8433484.html 题目传送门 - HDU1693 题意概括 多回路经过所有格子的方案数. 做法 最基础的插头 ...

  2. hdu1693 Eat the Trees [插头DP经典例题]

    想当初,我听见大佬们谈起插头DP时,觉得插头DP是个神仙的东西. 某大佬:"考场见到插头DP,直接弃疗." 现在,我终于懂了他们为什么这么说了. 因为-- 插头DP很毒瘤! 为什么 ...

  3. HDU 1693 Eat the Trees(插头DP)

    题目链接 USACO 第6章,第一题是一个插头DP,无奈啊.从头看起,看了好久的陈丹琦的论文,表示木看懂... 大体知道思路之后,还是无法实现代码.. 此题是插头DP最最简单的一个,在一个n*m的棋盘 ...

  4. hdu 1693 Eat the Trees——插头DP

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1693 第一道插头 DP ! 直接用二进制数表示状态即可. #include<cstdio> # ...

  5. HDU 1693 Eat the Trees ——插头DP

    [题目分析] 吃树. 直接插头DP,算是一道真正的入门题目. 0/1表示有没有插头 [代码] #include <cstdio> #include <cstring> #inc ...

  6. hdu1693 Eat the Trees 【插头dp】

    题目链接 hdu1693 题解 插头\(dp\) 特点:范围小,网格图,连通性 轮廓线:已决策点和未决策点的分界线 插头:存在于网格之间,表示着网格建的信息,此题中表示两个网格间是否连边 状态表示:当 ...

  7. hdu1693:eat trees(插头dp)

    题目大意: 题目背景竟然是dota!屠夫打到大后期就没用了,,只能去吃树! 给一个n*m的地图,有些格子是不可到达的,要把所有可到达的格子的树都吃完,并且要走回路,求方案数 题解: 这题大概是最简单的 ...

  8. [Hdu1693]Eat the Trees(插头DP)

    Description 题意:在n*m(1<=N, M<=11 )的矩阵中,有些格子有树,没有树的格子不能到达,找一条或多条回路,吃完所有的树,求有多少种方法. Solution 插头DP ...

  9. 2019.01.23 hdu1693 Eat the Trees(轮廓线dp)

    传送门 题意简述:给一个有障碍的网格图,问用若干个不相交的回路覆盖所有非障碍格子的方案数. 思路:轮廓线dpdpdp的模板题. 同样是讨论插头的情况,只不过没有前一道题复杂,不懂的看代码吧. 代码: ...

随机推荐

  1. html--添加、删除滚动条

    1:若想给div添加滚动条: style="overflow-y:scroll";(添加纵向滚动条) style="overflow-x:scroll";(添加 ...

  2. go--常量&运算符

    常量const 1.常量声明: const ( a = b c ) fmt.Println(a) fmt.Println(b) fmt.Println(c)// ======222 ====: 2.并 ...

  3. Django实现的博客系统中使用富文本编辑器ckeditor

    操作系统为OS X 10.9.2,Django为1.6.5. 1.下载和安装 1.1 安装 ckeditor 下载地址 https://github.com/shaunsephton/django-c ...

  4. Heavy Transportation(最短路)

    poj 1797 ——Heavy Transportation 思路: 这道题我们可以采用类似于求最短路径的方法,用一种新的“松弛操作”去取代原本的方法. 我们可以记录d[u]为运送货物到点j时最大可 ...

  5. BZOJ1017魔兽地图DotR 樹形DP

    @(BZOJ)[樹形DP, 三維DP] Description DotR (Defense of the Robots) Allstars是一个风靡全球的魔兽地图,他的规则简单与同样流行的地图DotA ...

  6. java多线程异步执行

    import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.ut ...

  7. Go -- PipleLine

    1.pipeline的产生 从一个现象说起,有一家咖啡吧生意特别好,每天来的客人络绎不绝,客人A来到柜台,客人B紧随其后,客人C排在客人B后面,客人D排在客人C后面,客人E排在客人D后面,一直排到店面 ...

  8. Answer&#39;s Question about pointer

    When you create a new pointer, this will be in heap until you delete it.  So what you said is sort o ...

  9. Raspberry Pi学习笔记

    一.树莓派 Raspberry Pi 更换国内源 编辑 /etc/apt/sources.list 文件,用 nano 命令编辑 pi@raspberrypi:~$ sudo cp /etc/apt/ ...

  10. poj 1659 Frogs&#39; Neighborhood 度序列可图化 贪心

    题意: 对一个无向图给出一个度序列,问他是否可简单图化. 分析: 依据Havel定理,直接贪心就可以. 代码: //poj 1659 //sep9 #include <iostream> ...