题目链接

hdu1693

题解

插头\(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】的更多相关文章

  1. HDU1693 Eat the Trees —— 插头DP

    题目链接:https://vjudge.net/problem/HDU-1693 Eat the Trees Time Limit: 4000/2000 MS (Java/Others)    Mem ...

  2. HDU1693 Eat the Trees 插头dp

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

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

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

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

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

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

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

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

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

  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. unity图形圆形展开

    脚本如下: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngi ...

  2. Linux TCP/IP调优参数 /proc/sys/net/目录

    所有的TCP/IP调优参数都位于/proc/sys/net/目录. 例如, 下面是最重要的一些调优参数,后面是它们的含义: /proc/sys/net/core/rmem_default " ...

  3. 关闭会声会影2018提示UEIP.dll找不到指定模块

    最近有一些会声会影2018用户反映在关闭后弹出UEIP.dll错误,不知道该怎么办才好,针对这个问题,小编下面为大家介绍下解决方法. 原因分析 出现这个错误跟会声会影安装路径有中文字符是密切相关的,导 ...

  4. 【python 2.7】python读取json数据存入MySQL

    同上一篇,只是适配 CentOS+ python 2.7 #python 2.7 # -*- coding:utf-8 -*- __author__ = 'BH8ANK' import json im ...

  5. Java实现在线预览功能

    java实现在线预览功能,需要用到  jacob.dll jacob.jar   预览pdf所需js  pdfobject.min.js 将上传文件转为pdf保存. <div class=&qu ...

  6. 如何在 Debian 9 下安装 LEMP 和 WHMCS 7.5

    WHMCS 7.5 发布了,它开始支持 PHP 7.2,这里就写个简单的教程记录一下安装方式. 1.准备工作 首先,我们需要按照 在Debian 9 / Debian 8 下使用源安装方式安装 LEM ...

  7. VPS挂机赚美刀详细介绍–Alexamaster操作流程

    跟 vps 主机打交道时间长了,手里也渐渐积累了些闲置的 vps.让它们这么闲着吧,感觉有些浪费资源:用起来吧,暂时又没有好的项目.一直听说通过 vps挂机可以赚回主机成本,甚至可以盈利.正好这两天有 ...

  8. 【Python进阶】用 Python 统计字数

    问题描述: 用 Python 实现函数 count_words(),该函数输入字符串 s 和数字 n,返回 s 中 n 个出现频率最高的单词.返回值是一个元组列表,包含出现次数最高的 n 个单词及其次 ...

  9. ES6中Class的继承关系

    es5实现中,每个对象都有__proto__属性(也就是关系图中[[prototype]]属性),指向对应的构造函数的prototype.Class 作为构造函数的语法糖,同时有prototype属性 ...

  10. java面向对象的有序数组和无序数组的比较

    package aa; class Array{ //定义一个有序数组 private long[] a; //定义数组长度 private int nElems; //构造函数初始化 public ...