HDU 1693 Eat the Trees(插头DP,入门题)
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))
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.
#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,入门题)的更多相关文章
- 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 ...
- HDU 1693 Eat the Trees(插头DP)
题目链接 USACO 第6章,第一题是一个插头DP,无奈啊.从头看起,看了好久的陈丹琦的论文,表示木看懂... 大体知道思路之后,还是无法实现代码.. 此题是插头DP最最简单的一个,在一个n*m的棋盘 ...
- hdu1693 Eat the Trees [插头DP经典例题]
想当初,我听见大佬们谈起插头DP时,觉得插头DP是个神仙的东西. 某大佬:"考场见到插头DP,直接弃疗." 现在,我终于懂了他们为什么这么说了. 因为-- 插头DP很毒瘤! 为什么 ...
- HDU 1693 Eat the Trees(插头DP、棋盘哈密顿回路数)+ URAL 1519 Formula 1(插头DP、棋盘哈密顿单回路数)
插头DP基础题的样子...输入N,M<=11,以及N*M的01矩阵,0(1)表示有(无)障碍物.输出哈密顿回路(可以多回路)方案数... 看了个ppt,画了下图...感觉还是挺有效的... 参考 ...
- HDU1693 Eat the Trees 插头dp
原文链接http://www.cnblogs.com/zhouzhendong/p/8433484.html 题目传送门 - HDU1693 题意概括 多回路经过所有格子的方案数. 做法 最基础的插头 ...
- hdu 1693 : Eat the Trees 【插头dp 入门】
题目链接 题意: 给出一个n*m大小的01矩阵,在其中画线连成封闭图形,其中对每一个值为1的方格,线要恰好穿入穿出共两次,对每一个值为0的方格,所画线不能经过. 参考资料: <基于连通性状态压缩 ...
- HDU - 1693 Eat the Trees(多回路插头DP)
题目大意:要求你将全部非障碍格子都走一遍,形成回路(能够多回路),问有多少种方法 解题思路: 參考基于连通性状态压缩的动态规划问题 - 陈丹琦 下面为代码 #include<cstdio> ...
- HDU 1693 Eat the Trees (插头DP)
题意:给一个n*m的矩阵,为1时代表空格子,为0时代表障碍格子,问如果不经过障碍格子,可以画一至多个圆的话,有多少种方案?(n<12,m<12) 思路: 这题不需要用到最小表示法以及括号表 ...
随机推荐
- 表达式过滤器 lowercase
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- sed实现路径替换
shell和sed忘得差不多了,现在更喜欢用python notebook,可以自动补充,所以很方便.但是记得以前用过这个的,试了几次不成功,搜了一下,这几个地方给的比较清晰,尤其是路径替换. 以下内 ...
- 使用fir.im和蒲公英进行测试的一些注意事项
前言:使用fir.im和蒲公英进行测试的一些注意事项 最近公司的项目遇到了一个问题,有的用户的手机系统版本低于9.3高于9.0的存在崩溃的情况,8.x的系统的用户的有的界面的显示有问题(比如说图片严重 ...
- spring(一)-基本概念
1.定义与特点 定义:一个分模块的一站式后台开发框架. 特征: (1)比起EJB,更轻量级别的容器框架,模块形式组织,只需要调用相应模块(jdbc.springmvc) (2)Spring IOC低耦 ...
- WKWebView进度及title
WKWebView进度及title WKWebView进度及title WKWebView 的estimatedProgress和title 都是KVO模式,所以可以添加监控: [webView ad ...
- 安装Chrome插件
重装系统后重装了Chrome浏览器,想着将自己的东西同步到Chrome上.但是登录谷歌账号要FQ,我又是靠setup插件FQ,但插件又要靠账号同步到本地.形成了死循环. 其实重装系统前我将插件提取了, ...
- django项目创建requirements.txt文件
workon+虚拟环境名 然后输入 pip freeze > requirements.txt
- 关于ECharts内存泄漏问题
最近使用websocket加ECharts做了一个实时监控的功能,发现了一个比较严重的问题,就是浏览器运行一段时间就会非常卡,之前在ECharts官网运行官方实例“动态数据 + 时间坐标轴”时,也遇到 ...
- MySQL数据操作(DML)
表结构准备: mysql> CREATE TABLE student( -> sid INT PRIMARY KEY AUTO_INCREMENT, ), -> age INT, ) ...
- Laravel POST请求API接口 使用validate表单验证返回欢迎页
突然遇到的问题 就是使用Laravel进行开发API接口的时候 发现在表单验证不通过的时候返回了登录页 猜测问题应该是因为表单验证失败后进行了重定向导致的 因为返回状态码200 网上找了好久没找到 ...