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的更多相关文章

  1. HDU1693 Eat the Trees —— 插头DP

    题目链接:https://vjudge.net/problem/HDU-1693 Eat the Trees Time Limit: 4000/2000 MS (Java/Others)    Mem ...

  2. HDU1693 Eat the Trees 插头dp

    原文链接http://www.cnblogs.com/zhouzhendong/p/8433484.html 题目传送门 - HDU1693 题意概括 多回路经过所有格子的方案数. 做法 最基础的插头 ...

  3. hdu1693 Eat the Trees 【插头dp】

    题目链接 hdu1693 题解 插头\(dp\) 特点:范围小,网格图,连通性 轮廓线:已决策点和未决策点的分界线 插头:存在于网格之间,表示着网格建的信息,此题中表示两个网格间是否连边 状态表示:当 ...

  4. 2019.01.23 hdu1693 Eat the Trees(轮廓线dp)

    传送门 题意简述:给一个有障碍的网格图,问用若干个不相交的回路覆盖所有非障碍格子的方案数. 思路:轮廓线dpdpdp的模板题. 同样是讨论插头的情况,只不过没有前一道题复杂,不懂的看代码吧. 代码: ...

  5. HDU1693 Eat the Trees(zerojudge a228)

    传送门: https://zerojudge.tw/ShowProblem?problemid=a228 http://acm.hdu.edu.cn/showproblem.php?pid=1693 ...

  6. [Hdu1693]Eat the Trees(插头DP)

    Description 题意:在n*m(1<=N, M<=11 )的矩阵中,有些格子有树,没有树的格子不能到达,找一条或多条回路,吃完所有的树,求有多少种方法. Solution 插头DP ...

  7. hdu1693 Eat the Trees [插头DP经典例题]

    想当初,我听见大佬们谈起插头DP时,觉得插头DP是个神仙的东西. 某大佬:"考场见到插头DP,直接弃疗." 现在,我终于懂了他们为什么这么说了. 因为-- 插头DP很毒瘤! 为什么 ...

  8. 【HDU1693】Eat the Trees(插头dp)

    [HDU1693]Eat the Trees(插头dp) 题面 HDU Vjudge 大概就是网格图上有些点不能走,现在要找到若干条不相交的哈密顿回路使得所有格子都恰好被走过一遍. 题解 这题的弱化版 ...

  9. HDU 1693 Eat the Trees(插头DP、棋盘哈密顿回路数)+ URAL 1519 Formula 1(插头DP、棋盘哈密顿单回路数)

    插头DP基础题的样子...输入N,M<=11,以及N*M的01矩阵,0(1)表示有(无)障碍物.输出哈密顿回路(可以多回路)方案数... 看了个ppt,画了下图...感觉还是挺有效的... 参考 ...

随机推荐

  1. Google C++ 代码规范

    Google C++ Style Guide   Table of Contents Header Files Self-contained Headers The #define Guard For ...

  2. windows下bat批处理执行sql语句__Mysql

    直接上代码: @ECHO OFF SET dbhost=主机名(例如:127.0.0.1)SET dbuser=用户名(例如:root)SET dbpasswd=用户密码(例如:root)SET db ...

  3. Docker相关释义

    Docker相关释义 基础网站:http://www.runoob.com/docker/docker-tutorial.html Docker的思想来自于集装箱,集装箱解决了什么问题?在一艘大船上, ...

  4. spring coud Feign常用配置

    Ribbon配置 在Feign中配置Ribbon非常简单,直接在application.properties中配置即可,如: # 设置连接超时时间 ribbon.ConnectTimeout=500 ...

  5. Scrapy项目创建以及目录详情

    Scrapy项目创建已经目录详情 一.新建项目(scrapy startproject) 在开始爬取之前,必须创建一个新的Scrapy项目.进入自定义的项目目录中,运行下列命令: PS C:\scra ...

  6. Java连接MySQL数据库三种方法

    好久没有更新博客了!今天利用周目时学习了一下数据库mysql.介绍一下数据库的三种连接方式! 开发工具:Myeclipse MySQL5.6 MySQL连接驱动:mysql-connector-jav ...

  7. Log4Net帮助类

    工具类 using System; using System.Diagnostics; using log4net; namespace Trumgu_BI_PF.Util { public clas ...

  8. 【CF724F】Uniformly Branched Trees

    题意:询问n个点的每个非叶子点度数恰好等于d的不同构的无根树的数目. n≤1000,d≤10n≤1000,d≤10. 题解: 这题真的是一道非常好的题 首先考虑有根树 定义f[i][j][k]表示i个 ...

  9. C语言之指针变量

    菜单导航 1.指针变量 2.指针和数组 3.常量指针和指向常量的指针 4.指针和字符串的关系 5.数组越界造成的访问不属于自己的内存空间现象 6.引用数据类型和基本数据类型,形参和实参 7.字符串和字 ...

  10. c_数据结构_栈的实现

    #include<stdio.h> #include<stdlib.h> #define STACK_INIT_SIZE 100 #define STACKINCREMENT ...