[HDU1693]Eat the Trees
Description:
给出n*m的方格,有些格子不能铺线,其它格子必须铺,可以形成多个闭合回路。问有多少种铺法?
Hint:
\(n,m<=12\)
Solution:
与原来单回路那题转移方程有些不同,详见代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mxn=15,c[4]={0,-1,1,0};
struct data {
int key; ll val;
};
ll ans;
int n,m,t,ex,ey;
char mp[mxn][mxn];
unordered_map<int ,data > dp[3];
typedef unordered_map<int ,data >::iterator uit;
inline void copy(data x,int id) {dp[id][x.key<<2]=(data){x.key<<2,x.val};}
inline int get(int st,int x) {x<<=1; return (st>>x)&3;}
inline int md(int st,int x,int val) {x<<=1; return (st&(~(3<<x)))|(val<<x);}
inline int getl(int st,int x) {
int l=x,cnt=1;
while(cnt!=0) cnt+=c[get(st,--l)];
return l;
}
inline int getr(int st,int x) {
int r=x,cnt=-1;
while(cnt!=0) cnt+=c[get(st,++r)];
return r;
}
inline void update(int x,int y,data d)
{
int st=d.key; ll val=d.val;
int p=get(st,y),q=get(st,y+1);
if(mp[x][y]=='*') {
if(p==0&&q==0) dp[t^1][st]=(data){st,dp[t^1][st].val+val};
return ;
}
if(p==0&&q==0) {
if(x==n-1||y==m-1) return ;
int nst=md(st,y,1); nst=md(nst,y+1,2);
dp[t^1][nst]=(data){nst,dp[t^1][nst].val+val};
return ;
}
if(p==0||q==0) {
if(y<m-1) {
int nst=md(st,y,0); nst=md(nst,y+1,p+q);
dp[t^1][nst]=(data){nst,dp[t^1][nst].val+val};
}
if(x<n-1) {
int nst=md(st,y,p+q); nst=md(nst,y+1,0);
dp[t^1][nst]=(data){nst,dp[t^1][nst].val+val};
}
return ;
}
int nst=md(st,y,0); nst=md(nst,y+1,0);
if(p==1&&q==1) nst=md(nst,getr(st,y+1),1);
if(p==2&&q==2) nst=md(nst,getl(st,y),2);
if(p==1&&q==2) {
dp[t^1][nst]=(data){nst,dp[t^1][nst].val+val}; //新增回路
if(x==ex&&y==ey) ans+=val; //分次数统计答案
return ;
}
if(p==2&&q==1) {
dp[t^1][nst]=(data){nst,dp[t^1][nst].val+val}; //一右一左直接消掉括号
return ;
}
dp[t^1][nst]=(data){nst,dp[t^1][nst].val+val};
}
int main()
{
int T;
cin>>T;
while(T--) {
dp[0].clear(),dp[1].clear();
scanf("%d%d",&n,&m); int tp,flag=0; ans=0;
for(int i=0;i<n;++i)
for(int j=0;j<m;++j) {
scanf("%d",&tp);
if(tp==1) mp[i][j]='.',++flag;
else mp[i][j]='*';
}
for(int i=0;i<n;++i)
for(int j=0;j<m;++j)
if(mp[i][j]=='.') ex=i,ey=j;
t=0; dp[t][0]=(data){0,1ll};
for(int i=0;i<n;++i) {
dp[2].clear();
for(uit j=dp[t].begin();j!=dp[t].end();++j) copy((*j).second,2);
dp[t].clear();
for(uit j=dp[2].begin();j!=dp[2].end();++j) dp[t][(*j).second.key]=(*j).second;
for(int j=0;j<m;++j) {
dp[t^1].clear();
for(uit k=dp[t].begin();k!=dp[t].end();++k)
update(i,j,(*k).second);
t^=1;
}
}
if(!flag) {puts("1");continue;} //特判,数据有坑
printf("%lld\n",ans);
}
return 0;
}
[HDU1693]Eat the Trees的更多相关文章
- HDU1693 Eat the Trees —— 插头DP
题目链接:https://vjudge.net/problem/HDU-1693 Eat the Trees Time Limit: 4000/2000 MS (Java/Others) Mem ...
- HDU1693 Eat the Trees 插头dp
原文链接http://www.cnblogs.com/zhouzhendong/p/8433484.html 题目传送门 - HDU1693 题意概括 多回路经过所有格子的方案数. 做法 最基础的插头 ...
- hdu1693 Eat the Trees 【插头dp】
题目链接 hdu1693 题解 插头\(dp\) 特点:范围小,网格图,连通性 轮廓线:已决策点和未决策点的分界线 插头:存在于网格之间,表示着网格建的信息,此题中表示两个网格间是否连边 状态表示:当 ...
- 2019.01.23 hdu1693 Eat the Trees(轮廓线dp)
传送门 题意简述:给一个有障碍的网格图,问用若干个不相交的回路覆盖所有非障碍格子的方案数. 思路:轮廓线dpdpdp的模板题. 同样是讨论插头的情况,只不过没有前一道题复杂,不懂的看代码吧. 代码: ...
- HDU1693 Eat the Trees(zerojudge a228)
传送门: https://zerojudge.tw/ShowProblem?problemid=a228 http://acm.hdu.edu.cn/showproblem.php?pid=1693 ...
- [Hdu1693]Eat the Trees(插头DP)
Description 题意:在n*m(1<=N, M<=11 )的矩阵中,有些格子有树,没有树的格子不能到达,找一条或多条回路,吃完所有的树,求有多少种方法. Solution 插头DP ...
- hdu1693 Eat the Trees [插头DP经典例题]
想当初,我听见大佬们谈起插头DP时,觉得插头DP是个神仙的东西. 某大佬:"考场见到插头DP,直接弃疗." 现在,我终于懂了他们为什么这么说了. 因为-- 插头DP很毒瘤! 为什么 ...
- 【HDU1693】Eat the Trees(插头dp)
[HDU1693]Eat the Trees(插头dp) 题面 HDU Vjudge 大概就是网格图上有些点不能走,现在要找到若干条不相交的哈密顿回路使得所有格子都恰好被走过一遍. 题解 这题的弱化版 ...
- HDU 1693 Eat the Trees(插头DP、棋盘哈密顿回路数)+ URAL 1519 Formula 1(插头DP、棋盘哈密顿单回路数)
插头DP基础题的样子...输入N,M<=11,以及N*M的01矩阵,0(1)表示有(无)障碍物.输出哈密顿回路(可以多回路)方案数... 看了个ppt,画了下图...感觉还是挺有效的... 参考 ...
随机推荐
- 用 DocumentFormat.OpenXml 和Microsoft.Office.Interop.Word 写入或者读取word文件
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...
- js基本类型和字符串的具体应用
变量 JavaScript 是一种弱类型语言,javascript的变量类型由它的值来决定. 定义变量需要用关键字 'var' var a = 123; var b = 'asd'; //同时定义多个 ...
- 利用 Windows API Code Pack 修改音乐的 ID3 信息
朋友由于抠门 SD 卡买小了,结果音乐太多放不下,又不舍得再买新卡,不得已决定重新转码,把音乐码率压低一点,牺牲点音质来换空间(用某些人的话说,反正不是搞音乐的,听不出差别)… 结果千千静听(百度音乐 ...
- [转] Mongoose初使用总结
连接mongoose mongoose连接数据库有两种方式 第一种: 'use strict'; const mongoose = require('mongoose'); mongoose.conn ...
- Quartz.net 2.4.1 使用记录
项目需要开发一个调度任务工具,用于
- ASP.NET Web Api 2 接口API文档美化之Swagger
使用第三方提供的swgger ui 可有效提高 web api 接口列表的阅读性,并且可以在页面中测试服务接口. 但本人在查阅大量资料并进行编码测试后,发现大部分的swagger实例并不能有效运行.例 ...
- mysql数据库授权
mysql数据库授权所有人 grant all privileges on *.* to 'root'@'%' identified by 'password'; flush privileges; ...
- JMeter命令行参数汇总
jmeter.properties文件,默认是使用JMETER_HOME/bin目录下的jmeter.properties,如果用户自定义有其它的配置,在这里加上 #用法如下: -p user.p ...
- 通过impala更改Kudu表属性
开发人员可以通过更改表的属性来更改 Impala 与给定 Kudu 表相关的元数据.这些属性包括表名, Kudu 主地址列表,以及表是否由 Impala (内部)或外部管理. Rename an Im ...
- BZOJ3451 Tyvj1953 Normal 点分治 多项式 FFT
原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ3451.html 题目传送门 - BZOJ3451 题意 给定一棵有 $n$ 个节点的树,在树上随机点分 ...