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. papers-06-07

    A simple neural network module for relational reasoning Visual Interaction Networks 今天被这两篇文章的工作刷屏,说实 ...

  2. 指纹获取 Fingerprint2

    指纹插件  Fingerprint2 import Fingerprint2 from 'fingerprintjs2' new Fingerprint2().get(function(result, ...

  3. Django-rest-framework(七)swagger使用

    在我们接口开发完之后,需要交付给别人对接,在没有使用swagger的时候,我们需要单独编写一份api接口文档,由postman之类的工具进行请求得到返回的结果.而有了swagger之后,可以通过提取接 ...

  4. C++中的头文件(.h)和源文件(.cpp)都应该写什么?

    头文件(.h):写定义和声明写类的声明(包括类里面的成员和方法的声明).函数原型.#define常数等,但是一般来说不写具体的实现.注意: 1.在写头文件的时候需要注意,在开头和结尾处必须按照如下样式 ...

  5. 你不知道的javaScript笔记(6)

    语法 语句表达式 句子是完整表达某个意思的一组词,由一个或多个短语组成,他们之间由标点符号或者连接词连接起来. 语句相当于句子,表达式相当于短语,运算符则相当于标点符号和连接词. JavaScript ...

  6. LVS-DR模式实现调度负载

    本篇文章主要梳理一下LVS前端调度过程及用户请求过程 实验架构 准备工作 添加各主机路由联通主机通信 Client IP route add default gw 172.20.17.19 Route ...

  7. bootstrap实现分页

    bootstrap分页功能 写前端都会面临的一个问题就是分页,如果是纯js分页也是可以的,只是可能代码量比较大,所以今天写一个关于用bootstrap框架分页的例子,希望以后可以帮助到一些对这方面比较 ...

  8. js继承的几种方法和es6继承方法

        一.原型链继     1.基本思想     利用原型链来实现继承,超类的一个实例作为子类的原型     2.具体实现     function F() {}     //原型属性,原型方法: ...

  9. Vue.js中 computed 和 methods 的区别

    官方文档中已经有对其的解释了,在这里把我的理解记录一下Vue中的methods.watch.computed computed 的使用场景 HTML模板中的复杂逻辑表达式,为了防止逻辑过重导致不易维护 ...

  10. JS数组push一个对象

    这个是正确的数据添加对象 var dypieArr = []; var dyArr = []; var arrStr = ''; for(var i = 0; i < dataStreet.le ...