【HDU】1693:Eat the Trees【插头DP】
Eat the Trees
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5079 Accepted Submission(s):
2628
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))
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.
ways in one line. It is guaranteed, that it does not exceed 263 – 1.
Use the format in the sample.
6 3
1 1 1
1 0 1
1 1 1
1 1 1
1 0 1
1 1 1
2 4
1 1 1 1
1 1 1 1
Case 2: There are 2 ways to eat the trees.
如上图,每根红线代表的是插头DP中状态的每一位,特别的地方就在最后一位,那条横线,表示的是当前格子被更新时左边是否有向右的插头,而当前格子上面的竖线表示上面是否有向下的插头...
这样,插头DP的状态就比普通轮廓线多一位。
这两个位置就是转移的重点。
在当前格子上面有插头或者左边有插头,那么可以转移到当前向右或者向下;如果两个状态同时存在,那么将两个状态合并,不能新增插头;如果两个状态都不存在,并且没有障碍格,那么同时增加两个新插头。
第一列、最后一排、最后一列需要特判。
空间要开足!!!
Code
#include<bits/stdc++.h>
#define LL long long
using namespace std; int n, m, G[][], ti;
LL dp[][( << )]; int main() {
int T;
scanf("%d", &T);
while(T --) {
scanf("%d%d", &n, &m);
for(int i = ; i <= n; i ++)
for(int j = ; j <= m; j ++)
scanf("%d", &G[i][j]);
memset(dp, , sizeof(dp));
int now = ;
dp[now][] = ;
for(int i = ; i <= n; i ++) {
for(int j = ; j <= m; j ++) {
now ^= ;
memset(dp[now], , sizeof(dp[now]));
for(int s = ; s < ( << m + ); s ++) {
int pre = s & ( << m);
int las = s & ;
if(!G[i][j]) {
int ss = (s << );
if(!pre && !las) dp[now][ss] += dp[now ^ ][s];
} else {
if(j != ) {
if(pre && las) {
int ss = (s ^ pre ^ las) << ;
dp[now][ss] += dp[now ^ ][s];
}
if(pre && !las) {
int ss = (s ^ pre) << | ;
if(j != m) dp[now][ss] += dp[now ^ ][s];
ss = (s ^ pre) << | ;
if(i != n) dp[now][ss] += dp[now ^ ][s];
}
if(!pre && las) {
int ss = (s ^ las) << | ;
if(j != m) dp[now][ss] += dp[now ^ ][s];
ss = (s ^ las) << | ;
if(i != n) dp[now][ss] += dp[now ^ ][s];
}
if(!pre && !las) {
int ss = s << | ;
dp[now][ss] += dp[now ^ ][s];
}
} else if(j == && !las) {
if(pre) {
int ss = (s ^ pre) << | ;
if(i != n) dp[now][ss] += dp[now ^ ][s];
ss = (s ^ pre) << | ;
if(j != m) dp[now][ss] += dp[now ^ ][s];
} else {
if(i != n && j != m) {
int ss = s << | ;
dp[now][ss] += dp[now ^ ][s];
}
}
}
}
}
}
}
printf("Case %d: There are %lld ways to eat the trees.\n", ++ti, dp[now][]);
}
}
【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)
题目链接 USACO 第6章,第一题是一个插头DP,无奈啊.从头看起,看了好久的陈丹琦的论文,表示木看懂... 大体知道思路之后,还是无法实现代码.. 此题是插头DP最最简单的一个,在一个n*m的棋盘 ...
- HDU 1693 Eat the Trees ——插头DP
[题目分析] 吃树. 直接插头DP,算是一道真正的入门题目. 0/1表示有没有插头 [代码] #include <cstdio> #include <cstring> #inc ...
- 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,画了下图...感觉还是挺有效的... 参考 ...
- HDU - 1693 Eat the Trees(多回路插头DP)
题目大意:要求你将全部非障碍格子都走一遍,形成回路(能够多回路),问有多少种方法 解题思路: 參考基于连通性状态压缩的动态规划问题 - 陈丹琦 下面为代码 #include<cstdio> ...
- HDU 1693 Eat the Trees(插头DP,入门题)
Problem Description Most of us know that in the game called DotA(Defense of the Ancient), Pudge is a ...
- HDU 1693 Eat the Trees (插头DP)
题意:给一个n*m的矩阵,为1时代表空格子,为0时代表障碍格子,问如果不经过障碍格子,可以画一至多个圆的话,有多少种方案?(n<12,m<12) 思路: 这题不需要用到最小表示法以及括号表 ...
- hdu 1693 : Eat the Trees 【插头dp 入门】
题目链接 题意: 给出一个n*m大小的01矩阵,在其中画线连成封闭图形,其中对每一个值为1的方格,线要恰好穿入穿出共两次,对每一个值为0的方格,所画线不能经过. 参考资料: <基于连通性状态压缩 ...
- HDU 1693 Eat the Trees
第一道(可能也是最后一道)插头dp.... 总算是领略了它的魅力... #include<iostream> #include<cstdio> #include<cstr ...
随机推荐
- python小工具之读取host文件
# -*- coding: utf-8 -*- # @Time : 2018/9/12 21:09 # @Author : cxa # @File : readhostfile.py # @Softw ...
- Vue 3.0 的生命周期
new Vue() new一个vue实例化对象 init Event & Lifecycle 执行一些初始化和生命周期相关的操作 beforeCreate 组件实例刚刚被创建出来 执行一些初始 ...
- 003_Mac挂载NTFS移动硬盘读取VMware虚拟机文件
一.Mac 挂载NTFS移动硬盘进行读写操作 (Read-only file system) 注意如下图所示先卸载,然后按照下图的命令进行挂载.然后cd /opt/003_vm/ &&am ...
- [转]关于MyEclipse下的项目无法使用BASE64Encoder问题的解决办法
[链接] http://blog.csdn.net/longlonglongchaoshen/article/details/75087616
- HTML 多张图片无缝连接
<table border="0" cellspacing="0" cellpadding="0" style="heigh ...
- Flask:初见
Windows 10家庭中文版,Python 3.6.4 从Flask官网开始学起. 介绍 Flask是一个Python的Web开发微框架,基于Werkzeug.Jinja2模块(and good i ...
- Vue 实现loading进度条
项目中遇到的,用vue实现下: <template> <div class="plLoading"> <div class="plLoadi ...
- 洛谷P2296寻找道路
传送门啦 题目中有一个条件是路径上的所有点的出边所指向的点都直接或间接与终点连通. 所以我们要先判断能否走这一个点, $ bfs $ 类似 $ spfa $ 的一个判断,打上标记. 在这我反向建图,最 ...
- [android]Intent跳转新的Activity可以传递数据过去
两种方式: 一,直接通过Bundle对象来传递: 如果我们想要给“收件人”Activity说点什么的话,那么可以通过下面这封“E-mail”来将我们的消息传递出去 Intent intent=new ...
- malloc和free的实现
C++ Code 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 ...