P5074 Eat the Trees
思路
同样是插头DP,但是这题因为可以形成多个回路,所以左右括号是没有区别的,只需要01就可以表示了
注意if的嵌套关系
注意全零矩阵也要输出1
代码
#include <cstdio>
#include <cstring>
#include <algorithm>
#define int long long
using namespace std;
const int HASHsize = 400000;
int mat[20][20],n,m,T,pos[40];
int cnt[2],now,last,ans,endx,endy,fir[HASHsize+10],nxt[HASHsize+10],val[2][HASHsize+10],times[2][HASHsize+10];
void init(void){
memset(mat,0,sizeof(mat));
memset(cnt,0,sizeof(cnt));
now=0,last=0,ans=0,endx=0,endy=0;
memset(fir,0,sizeof(fir));
memset(nxt,0,sizeof(nxt));
memset(val,0,sizeof(val));
memset(times,0,sizeof(times));
}
void insert(int c,int num){
int t=c%HASHsize;
for(int i=fir[t];i;i=nxt[i]){
if(val[now][i]==c){
times[now][i]+=num;
return;
}
}
++cnt[now];
val[now][cnt[now]]=c;
times[now][cnt[now]]=num;
nxt[cnt[now]]=fir[t];
fir[t]=cnt[now];
}
void print(int x){
for(int i=0;i<m+1;i++)
printf("%lld",(x>>i)&1);
printf("\n");
}
void dp(void){
now=0;
insert(0,1);
for(int i=1;i<=n;i++){
for(int k=1;k<=cnt[now];k++)
val[now][k]<<=1;
for(int j=1;j<=m;j++){
last=now;
now^=1;
cnt[now]=0;
memset(fir,0,sizeof(fir));
memset(nxt,0,sizeof(nxt));
// printf("i=%lld j=%lld\n",i,j);
for(int k=1;k<=cnt[last];k++){
int state=val[last][k],num=times[last][k],plugL=(state>>(j-1))&1,plugU=(state>>(j))&1;
// printf("num=%lld\n",num);
// print(state);
// printf("plugL=%lld plugU=%lld\n",plugL,plugU);
if(mat[i][j]){
//新建连通分量
if((!plugL)&&(!plugU)){
if(mat[i+1][j]&&mat[i][j+1])
insert(state+(1<<(j-1))+(1<<(j)),num);
}
//合并联通分量
else if(plugL&&plugU){
if(i==endx&&j==endy)
ans+=num;
else
insert(state-(1<<(j-1))-(1<<(j)),num);
}
//延续联通分量
else{
if(plugL){
// printf("!\n");
if(mat[i+1][j])
insert(state,num);
if(mat[i][j+1])
insert(state-(1<<(j-1))+(1<<(j)),num);
}
if(plugU){
if(mat[i+1][j])
insert(state-(1<<(j))+(1<<(j-1)),num);
if(mat[i][j+1])
insert(state,num);
}
}
}
else{
if((!plugL)&&(!plugU))
insert(state,num);
}
}
}
}
}
signed main(){
scanf("%lld",&T);
pos[0]=1;
for(int i=1;i<30;i++)
pos[i]=pos[i-1]<<1;
while(T--){
bool isok=true;
init();
scanf("%lld %lld",&n,&m);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
scanf("%lld",&mat[i][j]);
if(mat[i][j]){
endx=i;endy=j;
isok=false;
}
}
// printf("endx=%lld endy=%lld\n",endx,endy);
if(!isok){
dp();
printf("%lld\n",ans);
}
else{
printf("1\n");
}
}
return 0;
}
P5074 Eat the Trees的更多相关文章
- 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
http://acm.hdu.edu.cn/showproblem.php?pid=1693 题意:n×m的棋盘求简单回路(可以多条)覆盖整个棋盘的方案,障碍格不许摆放.(n,m<=11) #i ...
- HDU 1693 Eat the Trees(插头DP)
题目链接 USACO 第6章,第一题是一个插头DP,无奈啊.从头看起,看了好久的陈丹琦的论文,表示木看懂... 大体知道思路之后,还是无法实现代码.. 此题是插头DP最最简单的一个,在一个n*m的棋盘 ...
- HDU 1693 Eat the Trees
第一道(可能也是最后一道)插头dp.... 总算是领略了它的魅力... #include<iostream> #include<cstdio> #include<cstr ...
- 【HDOJ】【1693】Eat The Trees
插头DP 插头dp模板题…… 这题比CDQ论文上的例题还要简单……因为不用区分左右插头(这题可以多回路,并不是一条哈密尔顿路) 硬枚举当前位置的状态就好了>_< 题解:http://blo ...
- Eat the Trees hdu 1693
Problem DescriptionMost of us know that in the game called DotA(Defense of the Ancient), Pudge is a ...
- HDU - 1693 Eat the Trees(多回路插头DP)
题目大意:要求你将全部非障碍格子都走一遍,形成回路(能够多回路),问有多少种方法 解题思路: 參考基于连通性状态压缩的动态规划问题 - 陈丹琦 下面为代码 #include<cstdio> ...
- HDU1693 Eat the Trees 插头dp
原文链接http://www.cnblogs.com/zhouzhendong/p/8433484.html 题目传送门 - HDU1693 题意概括 多回路经过所有格子的方案数. 做法 最基础的插头 ...
- 【HDU1693】Eat the Trees(插头dp)
[HDU1693]Eat the Trees(插头dp) 题面 HDU Vjudge 大概就是网格图上有些点不能走,现在要找到若干条不相交的哈密顿回路使得所有格子都恰好被走过一遍. 题解 这题的弱化版 ...
随机推荐
- eNSP使用-不同网段的互联
就像下面这个场景: 1.基本配置 先点击左上角的——新建 然后咱们把要用的设备都拖到面板上去 成品就是这样的: 点击这个为他们添加备注 我们来配置一下实验编址 右键单击PC1设置(PC2同理,就不多演 ...
- 运算符优先级 以及 && (逻辑与) 和||(逻辑或)的优先级:
运算符优先级(从高到低列出) 运算符 描述 . [] () 字段访问.数组下标.函数调用以及表达式分组 ++ -- - ~ ! delete new typeof void 一元运算符.返回数据类型. ...
- (十四)springMvc 对 restful 的支持
文章目录 restful 的概念 配置支持 restful 的前端控制器 配置不拦截静态资源 restful 的概念 restful 是一种开发理念: 对 url 进行规范 每个 url 代表一个资源 ...
- Devexpress xaf BO中字段为RuleRequiredField必输字段时,文本标签默认添加*标记
BO中字段为RuleRequiredField必输字段时,文本标签默认添加*标记.需要在模型编辑器中设置,如图. 官网地址:https://docs.devexpress.com/eXpressApp ...
- 【AC自动机】单词
[题目链接] https://loj.ac/problem/10060 [题意] 某人读论文,一篇论文是由许多单词组成.但他发现一个单词会在论文中出现很多次,现在想知道每个单词分别在论文中出现多少次. ...
- k8s之网络插件flannel及基于Calico的网络策略
1.k8s网络通信 a.容器间通信:同一个pod内的多个容器间的通信,通过lo即可实现; b.pod之间的通信:pod ip <---> pod ip,pod和pod之间不经过任何转换即可 ...
- volatile 关键字(修饰变量)
目录 volatile 关键字(修饰变量) 1. 含义 2. 作用 3. 如何保证可见性 4. 如何禁止指令重排序优化 5. volatile 是不安全的 6. volatile 不适用场景 vola ...
- IOS 改变UISearchBar的背景色
之前网上提供的方法试了很多种 都不能很好的去掉背景色 ,修改背景色方法如下: searchbar.barStyle = UIBarStyleBlackTranslucent; searchbar. ...
- jvm常用命令
jps // 查看Java进程ID和main方法类名 jstack <进程ID> // 查看该进程的所有栈信息 jstack -l <进程ID> // 查看该进程的所有栈信息, ...
- h5嵌套iframe实时传参(适用vue)
今天看到一个同事研究给iframe传参,由于好奇,我自己也写了个demo,说起来其实也挺简单的,但是在此之前没有用过,便想记录一下 其中主要用到的是postMessage 在页面中引入一个iframe ...