hdu1693 Eat the Trees 【插头dp】
题目链接
题解
插头\(dp\)
特点:范围小,网格图,连通性
轮廓线:已决策点和未决策点的分界线
插头:存在于网格之间,表示着网格建的信息,此题中表示两个网格间是否连边
状态表示:当前点\((i,j)\)和轮廓线上\(m + 1\)个插头的状态
状态转移:

我们用\(f[i][j][s]\)表示如上的状态,最后一次决策点为\((i,j)\),轮廓线上插头状态为\(s\)的方案数
比如上图\(s = 1101001\)
之后我们扩展新的点,枚举它插头的状态进行转移

在本题中,要使最终形成若干回路,每个点度数必须为\(2\),所以我们扩展点的时候记录它已有的插头数,然后剩余的插头数就可以唯一确定
然后就可以\(O(nm2^m)\)过了这道插头\(dp\)入门题
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 12,maxm = 100005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int n,m,S[maxn][maxn];
LL f[maxn][maxn][1 << maxn];
void work(int C){
cls(f);
if (!S[1][1]) f[1][1][0] = 1;
else {
if (S[1][2] == 0 || S[2][1] == 0){
printf("Case %d: There are 0 ways to eat the trees.\n",C);
return;
}
f[1][1][3] = 1;
}
int maxv = (1 << m + 1) - 1,cnt,e,t;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= m; j++){
if (i == n && j == m) break;
for (int s = 0; s <= maxv; s++){
if (!f[i][j][s]) continue;
if (j == m){
if (s & 1){
if (i + 2 <= n && S[i + 2][1])
f[i + 1][1][(s >> 1) << 2 | 1] += f[i][j][s];
if (S[i + 1][2])
f[i + 1][1][(s >> 1) << 2 | 2] += f[i][j][s];
}
else {
if (!S[i + 1][1]) f[i + 1][1][(s >> 1) << 2] += f[i][j][s];
else {
if (i + 2 > n || !S[i + 2][1] || !S[i + 1][2]) continue;
f[i + 1][1][(s >> 1) << 2 | 3] += f[i][j][s];
}
}
}
else {
cnt = ((s >> j) & 1) + ((s >> j + 1) & 1);
t = (s >> j) & 3; e = s ^ (t << j);
if (cnt && !S[i][j + 1]) continue;
if (cnt == 2) f[i][j + 1][e] += f[i][j][s];
else if (cnt == 1){
if (i + 1 <= n && S[i + 1][j + 1])
f[i][j + 1][e | (1 << j)] += f[i][j][s];
if (j + 2 <= m && S[i][j + 2])
f[i][j + 1][e | (1 << j + 1)] += f[i][j][s];
}
else {
if (!S[i][j + 1]) f[i][j + 1][e] += f[i][j][s];
else {
if (i + 1 > n || j + 2 > m || !S[i + 1][j + 1] || !S[i][j + 2])
continue;
f[i][j + 1][e | (3 << j)] += f[i][j][s];
}
}
}
}
}
}
LL ans = 0;
for (int s = 0; s <= maxv; s++)
ans += f[n][m][s];
printf("Case %d: There are %lld ways to eat the trees.\n",C,ans);
}
int main(){
int T = read();
REP(t,T){
n = read(); m = read();
REP(i,n) REP(j,m) S[i][j] = read();
if (n == 1 || m == 1){
if (!S[n][m]) printf("Case %d: There are 1 ways to eat the trees.\n",t);
else printf("Case %d: There are 0 ways to eat the trees.\n",t);
continue;
}
work(t);
}
return 0;
}
hdu1693 Eat the Trees 【插头dp】的更多相关文章
- HDU1693 Eat the Trees —— 插头DP
题目链接:https://vjudge.net/problem/HDU-1693 Eat the Trees Time Limit: 4000/2000 MS (Java/Others) Mem ...
- HDU1693 Eat the Trees 插头dp
原文链接http://www.cnblogs.com/zhouzhendong/p/8433484.html 题目传送门 - HDU1693 题意概括 多回路经过所有格子的方案数. 做法 最基础的插头 ...
- hdu1693 Eat the Trees [插头DP经典例题]
想当初,我听见大佬们谈起插头DP时,觉得插头DP是个神仙的东西. 某大佬:"考场见到插头DP,直接弃疗." 现在,我终于懂了他们为什么这么说了. 因为-- 插头DP很毒瘤! 为什么 ...
- HDU 1693 Eat the Trees(插头DP)
题目链接 USACO 第6章,第一题是一个插头DP,无奈啊.从头看起,看了好久的陈丹琦的论文,表示木看懂... 大体知道思路之后,还是无法实现代码.. 此题是插头DP最最简单的一个,在一个n*m的棋盘 ...
- hdu 1693 Eat the Trees——插头DP
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1693 第一道插头 DP ! 直接用二进制数表示状态即可. #include<cstdio> # ...
- HDU 1693 Eat the Trees ——插头DP
[题目分析] 吃树. 直接插头DP,算是一道真正的入门题目. 0/1表示有没有插头 [代码] #include <cstdio> #include <cstring> #inc ...
- hdu1693:eat trees(插头dp)
题目大意: 题目背景竟然是dota!屠夫打到大后期就没用了,,只能去吃树! 给一个n*m的地图,有些格子是不可到达的,要把所有可到达的格子的树都吃完,并且要走回路,求方案数 题解: 这题大概是最简单的 ...
- [Hdu1693]Eat the Trees(插头DP)
Description 题意:在n*m(1<=N, M<=11 )的矩阵中,有些格子有树,没有树的格子不能到达,找一条或多条回路,吃完所有的树,求有多少种方法. Solution 插头DP ...
- 2019.01.23 hdu1693 Eat the Trees(轮廓线dp)
传送门 题意简述:给一个有障碍的网格图,问用若干个不相交的回路覆盖所有非障碍格子的方案数. 思路:轮廓线dpdpdp的模板题. 同样是讨论插头的情况,只不过没有前一道题复杂,不懂的看代码吧. 代码: ...
随机推荐
- VBA_常用VBA代码
'批量替换字符 Sub Test() Dim i As Integer ).Value = "已激活" Then Cells(i, ).Value = "Active&q ...
- NO.02---聊聊Vue提升
如果本篇有看不明白的地方,请翻阅上一篇文章 上一篇我们讲了如何通过一些简单的动作来改变 store.js 中的数据对象,在实际工作中,这是完全无法满足工作需求的,所以这篇我们来说说如何做一些简单的流程 ...
- 智慧树mooc自动刷课代码
最近学习javaScript和JQuery,恰好还有一门mooc没有看.结合学习的知识和其他人的代码:撸了一个自动播放课程的代码,同时自动跳过单章的测试题. 用电脑挂着不动就完事了. 如下: var ...
- 【Python进阶】用 Python 统计字数
问题描述: 用 Python 实现函数 count_words(),该函数输入字符串 s 和数字 n,返回 s 中 n 个出现频率最高的单词.返回值是一个元组列表,包含出现次数最高的 n 个单词及其次 ...
- 20162328蔡文琛 Bag类
在刚刚开始着手这个作业时,想的是使用for循环来自己写出add等方法来,但是在看过API后知道了Arraylist这个java已有的列表类,于是就只用ArrayList的方法很快的就做了出来.在进行B ...
- 【树上DFS】Tree and Polynomials
http://codeforces.com/gym/101372 D push1[i][k]:所有操作1总共要让节点i下推多少系数k push2[i][k]:所有操作2总共要让节点i上推多少系数k s ...
- c++第三次作业
GitHub地址 https://github.com/ronghuijun/3Elevators-scheduling 实现过程 一开始打算分成三个类来写的 因为想到电梯的功能不太一样 一个只能上1 ...
- android入门 — Activity启动模式
1.standard模式 standard模式是系统的默认启动方式,每次激活Activity都会创建Activity,并放在任务栈中. 系统不会在乎活动是否已经存在于返回栈中,每次启动都会创建该活动的 ...
- lintcode-248-统计比给定整数小的数的个数
248-统计比给定整数小的数的个数 给定一个整数数组 (下标由 0 到 n-1,其中 n 表示数组的规模,数值范围由 0 到 10000),以及一个 查询列表.对于每一个查询,将会给你一个整数,请你返 ...
- this & super
/* 当本类的成员和局部变量同名用this区分. 当子父类中的成员变量同名用super区分父类. this和super的用法很相似. this:代表一个本类对象的引用. super:代表一个父 ...