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.
 
题目大意:用任意多个回路覆盖矩阵上的1.
思路:插头DP,参考IOI国家集训队论文,陈丹琦的《基于连通性状态压缩的动态规划问题》
 
代码(62MS)(普通推,一大堆无用状态):
 #include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long LL; const int MAXN = ; int mat[MAXN][MAXN];
LL dp[MAXN][MAXN][ << MAXN];
int n, m, T; LL solve() {
memset(dp, , sizeof(dp));
dp[][m][] = ;
for(int i = ; i <= n; ++i) {
for(int j = ; j < ( << m); ++j) dp[i][][j << ] = dp[i - ][m][j];
for(int k = ; k <= m; ++k) {
for(int state = ; state < ( << (m + )); ++state) {
int y = << k, x = y >> ;
if(mat[i][k]) {
if((state & x) && (state & y)) {
dp[i][k][state] = dp[i][k - ][state - x - y];
} else if((state & x) == && (state & y) == ) {
dp[i][k][state] = dp[i][k - ][state + x + y];
} else dp[i][k][state] = dp[i][k - ][state ^ x ^ y] + dp[i][k - ][state];
} else {
if((state & x) == && (state & y) == ) {
dp[i][k][state] = dp[i][k - ][state];
} else dp[i][k][state] = ;
}
}
}
}
return dp[n][m][];
} int main() {
scanf("%d", &T);
for(int t = ; t <= T; ++t) {
scanf("%d%d", &n, &m);
for(int i = ; i <= n; ++i)
for(int j = ; j <= m; ++j) scanf("%d", &mat[i][j]);
printf("Case %d: There are %I64d ways to eat the trees.\n", t, solve());
}
}

代码(0MS)(hash)(下面代码是lld的……):

 #include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long LL; const int MAXH = ;
const int SIZEH = ; struct hash_map {
int head[SIZEH];
int next[MAXH], state[MAXH];
LL val[MAXH];
int size; void init() {
memset(head, -, sizeof(head));
size = ;
} void insert(int st, LL sv) {
int h = st % SIZEH;
for(int p = head[h]; ~p; p = next[p]) {
if(state[p] == st) {
val[p] += sv;
return ;
}
}
state[size] = st; val[size] = sv; next[size] = head[h]; head[h] = size++;
}
} hashmap[]; int getB(int state, int i) {
return (state >> i) & ;
} void setB(int &state, int i, int val) {
state = (state & ~( << i)) | (val << i);
} int mat[][];
int n, m, T;
hash_map *cur, *last; void update(int state, LL val, int x, int y) {
int left = getB(state, y);
int up = getB(state, y + );
if(mat[x][y] == ) {
if(left == && up == ) cur->insert(state, val);
return ;
}
if(left == && up == ) {
if(x < n - && y < m - ) {
int newState = state;
setB(newState, y, );
setB(newState, y + , );
cur->insert(newState, val);
}
} else if(left == || up == ) {
if(x < n - ) {
int newState = state;
setB(newState, y, );
setB(newState, y + , );
cur->insert(newState, val);
}
if(y < m - ) {
int newState = state;
setB(newState, y, );
setB(newState, y + , );
cur->insert(newState, val);
}
} else {
int newState = state;
setB(newState, y, );
setB(newState, y + , );
cur->insert(newState, val);
}
} LL solve() {
cur = hashmap, last = hashmap + ;
last->init();
last->insert(, );
for(int i = ; i < n; ++i) {
int sz = last->size;
for(int k = ; k < sz; ++k) last->state[k] <<= ;
for(int j = ; j < m; ++j) {
cur->init();
sz = last->size;
for(int k = ; k < sz; ++k)
update(last->state[k], last->val[k], i, j);
swap(cur, last);
}
}
for(int k = ; k < last->size; ++k)
if(last->state[k] == ) return last->val[k];
return ;
} int main() {
scanf("%d", &T);
for(int t = ; t <= T; ++t) {
scanf("%d%d", &n, &m);
for(int i = ; i < n; ++i)
for(int j = ; j < m; ++j) scanf("%d", &mat[i][j]);
printf("Case %d: There are %lld ways to eat the trees.\n", t, solve());
}
}

HDU 1693 Eat the Trees(插头DP,入门题)的更多相关文章

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

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

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

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

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

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

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

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

  5. HDU 1693 Eat the Trees(插头DP、棋盘哈密顿回路数)+ URAL 1519 Formula 1(插头DP、棋盘哈密顿单回路数)

    插头DP基础题的样子...输入N,M<=11,以及N*M的01矩阵,0(1)表示有(无)障碍物.输出哈密顿回路(可以多回路)方案数... 看了个ppt,画了下图...感觉还是挺有效的... 参考 ...

  6. HDU1693 Eat the Trees 插头dp

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

  7. hdu 1693 : Eat the Trees 【插头dp 入门】

    题目链接 题意: 给出一个n*m大小的01矩阵,在其中画线连成封闭图形,其中对每一个值为1的方格,线要恰好穿入穿出共两次,对每一个值为0的方格,所画线不能经过. 参考资料: <基于连通性状态压缩 ...

  8. HDU - 1693 Eat the Trees(多回路插头DP)

    题目大意:要求你将全部非障碍格子都走一遍,形成回路(能够多回路),问有多少种方法 解题思路: 參考基于连通性状态压缩的动态规划问题 - 陈丹琦 下面为代码 #include<cstdio> ...

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

    题意:给一个n*m的矩阵,为1时代表空格子,为0时代表障碍格子,问如果不经过障碍格子,可以画一至多个圆的话,有多少种方案?(n<12,m<12) 思路: 这题不需要用到最小表示法以及括号表 ...

随机推荐

  1. 写到 HTML 文档

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  2. MQTT初始篇笔记整理

    MQTT简介 MQTT(Message Queuing Telemetry Transport,消息队列遥测传输),基于TCP/IP 协议栈而构建,虽然叫消息队列遥测传输,但是她与消息队列毫无关系,她 ...

  3. 全文搜索引擎 Elasticsearch 安装踩坑记录

    一.安装 Elastic 需要 Java 8 环境.如果你的机器还没安装 Java 安装完 Java,就可以跟着官方文档安装 Elastic.直接下载压缩包比较简单. $ wget https://a ...

  4. IOS 浅谈闭包block的使用

    前言:对于ios初学者,block通常用于逆向传值,遍历等,会使用,但是可能心虚,会感觉block很神秘,那么下面就一起来揭开它的面纱吧. ps: 下面重点讲叙了闭包的概念,常用的语法,以及访问变量, ...

  5. JDBC相关

    //原生jdbc操作案例 import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; i ...

  6. django-初始配置(纯手写)

    我们通过django-admin startproject zhuyu命令创建好项目后,在pycharm中打开 我们需要在在该项目中,配置一些相关操作. 1.template(存放模板的文件夹) 如果 ...

  7. 基于layer封装的异步加载分部视图弹出层

    背景:之前一直用的artdialog,但是样式不是很好看,后来偶然看到layer,觉得不错,但是对于.net mvc来说,不能像artdialog一样弹出分部视图是很难受的.所以下面的方法就解决了. ...

  8. JS高度融合入门笔记(一)

    复制下面的代码到编辑器里,让编辑器自动排版一下格式,效果会好一点,自我感觉我笔记的条理还是比较容易记忆的 <!DOCTYPE html><html><head> & ...

  9. CentOS 7.4使用yum源安装php7.2

    1.如果之前已经安装我们先卸载一下 yum -y remove php* 2.由于linux的yum源不存在php7.x,所以我们要更改yum源 rpm -Uvh https://dl.fedorap ...

  10. 【poe设备加电配置】

    开启接口的poe功能: [interface_name]: 配置poe端口的最大功率: [interface_name[: 配置poe的端口工作模式: [interface_name[: 配置poe端 ...