bzoj 1814 Ural 1519 Formula 1 插头DP
1814: Ural 1519 Formula 1
Time Limit: 1 Sec Memory Limit: 64 MB
Submit: 942 Solved: 356
[Submit][Status][Discuss]
Description
of the fact, that Vologda could not get rights to hold the Winter
Olympic games of 20**, it is well-known, that the city will conduct one
of the Formula 1 events. Surely, for such an important thing a new race
circuit should be built as well as hotels, restaurants, international
airport - everything for Formula 1 fans, who will flood the city soon.
But when all the hotels and a half of the restaurants were built, it
appeared, that at the site for the future circuit a lot of gophers lived
in their holes. Since we like animals very much, ecologists will never
allow to build the race circuit over the holes. So now the mayor is
sitting sadly in his office and looking at the map of the circuit with
all the holes plotted on it.
Problem
Who will be smart enough to draw a plan of the circuit and keep the city
from inevitable disgrace? Of course, only true professionals -
battle-hardened programmers from the first team of local technical
university!.. But our heroes were not looking for easy life and set much
more difficult problem: "Certainly, our mayor will be glad, if we find
how many ways of building the circuit are there!" - they said.
It should be said, that the circuit in Vologda is going to be rather
simple. It will be a rectangle N*M cells in size with a single circuit
segment built through each cell. Each segment should be parallel to one
of rectangle's sides, so only right-angled bends may be on the circuit.
At the picture below two samples are given for N = M = 4 (gray squares
mean gopher holes, and the bold black line means the race circuit).
There are no other ways to build the circuit here.
一个 m * n 的棋盘,有的格子存在障碍,求经过所有非障碍格子的哈密顿回路个数
Input
integer numbers N and M (2 ≤ N, M ≤ 12). Each of the next N lines
contains M characters, which are the corresponding cells of the
rectangle. Character "." (full stop) means a cell, where a segment of
the race circuit should be built, and character "*" (asterisk) - a cell,
where a gopher hole is located.
Output
Sample Input
**..
....
....
....
Sample Output
HINT
Source
题解:什么叫做哈密顿回路,就是从一个点出发,经过所有点一次仅一次,
所有可以知道路不能交叉。
具体还是看cdqhttps://wenku.baidu.com/view/9cfbb16e011ca300a6c390d5.html
#pragma GCC optimize(2)
#pragma G++ optimize(2)
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring> #define sz 200005
#define ll long long
using namespace std; int hs[sz],tot[],sta[][sz],bit[];
int n,m,c,ex,ey,mp[][];
ll dp[][sz],ans;
char s[]; void add(int s,ll num)
{
int pos=s%sz;
while (hs[pos]!=-)
{
if (sta[c][hs[pos]]==s)
{
dp[c][hs[pos]]+=num;
return;
}
pos++;
if (pos==sz) pos=;
}
dp[c][++tot[c]]=num;
hs[pos]=tot[c];
sta[c][tot[c]]=s;
}
void DP()
{
for (int i=;i<=n;i++)
{
for (int k=;k<=tot[c];k++)
sta[c][k]<<=;
for (int j=;j<=m;j++)
{
c^=; tot[c]=;
memset(hs,-,sizeof(hs));
for (int k=;k<=tot[c^];k++)
{
int s=sta[c^][k];
ll num=dp[c^][k];
int p=(s>>bit[j-])&,q=(s>>bit[j])&;
if (!mp[i][j])
{
if (!p&&!q) add(s,num);
}
else if (!p&&!q)
{
if (!mp[i+][j]||!mp[i][j+]) continue;
s+=(<<bit[j-])+(<<(bit[j]+)) ;
add(s,num);
}
else if (!p&&q){
if (mp[i][j+]) add(s,num);
if (mp[i+][j]){
s+=(<<bit[j-])*q-(<<bit[j])*q;
add(s,num);
}
}
else if (!q&&p){
if (mp[i+][j]) add(s,num);
if (mp[i][j+]){
s+=(<<bit[j])*p-(<<bit[j-])*p;
add(s,num);
}
}
else if (p+q==){
int b=;
for (int t=j+;t<=m;t++){
int v=(s>>bit[t])&;
if (v==) b++;
if (v==) b--;
if (!b){
s-=(<<bit[t]);
break;
}
}
s-=(<<bit[j-])+(<<bit[j]);
add(s,num);
}
else if (p+q==){
int b=;
for (int t=j-;t>=;t--){
int v=(s>>bit[t])&;
if (v==) b++;
if (v==) b--;
if (!b){
s+=(<<bit[t]);
break;
}
}
s-=*(<<bit[j-])+*(<<bit[j]);
add(s,num);
}
else if (p==&&q==){
s-=(<<(bit[j-]+))+(<<bit[j]);
add(s,num);
}
else if (p==&&q==){
if (i==ex&&j==ey) ans+=num;
}
}
}
}
}
int main(){
for (int i=;i<=;i++)
bit[i]=i<<;
scanf("%d%d",&n,&m);
tot[]=; dp[c][]=;
for (int i=;i<=n;i++){
scanf("%s",s+);
for (int j=;j<=m;j++)
if (s[j]=='.') mp[i][j]=,ex=i,ey=j;
}
DP();
printf("%lld\n",ans);
}
那么对于这道题目,分析其特殊性质。
bzoj 1814 Ural 1519 Formula 1 插头DP的更多相关文章
- bzoj 1814 Ural 1519 Formula 1 ——插头DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1814 普通的插头 DP .但是调了很久.注意如果合并两个 1 的话,不是 “把向右第一个 2 ...
- bzoj 1814: Ural 1519 Formula 1 插头dp经典题
用的括号序列,听说比较快. 然并不会预处理,只会每回暴力找匹配的括号. #include<iostream> #include<cstdio> #include<cstr ...
- bzoj1814 Ural 1519 Formula 1(插头dp模板题)
1814: Ural 1519 Formula 1 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 924 Solved: 351[Submit][Sta ...
- 【BZOJ1814】Ural 1519 Formula 1 插头DP
[BZOJ1814]Ural 1519 Formula 1 题意:一个 m * n 的棋盘,有的格子存在障碍,求经过所有非障碍格子的哈密顿回路个数.(n,m<=12) 题解:插头DP板子题,刷板 ...
- Ural 1519 Formula 1 插头DP
这是一道经典的插头DP单回路模板题. 用最小表示法来记录连通性,由于二进制的速度,考虑使用8进制. 1.当同时存在左.上插头的时候,需要判断两插头所在连通块是否相同,若相同,只能在最后一个非障碍点相连 ...
- bzoj 1814: Ural 1519 Formula 1【插头dp】
设f[i][j][s]为轮廓线推到格子(i,j),状态为s的方案数 括号表示一段线的左端和右端,表示成左括号和右括号,状压的时候用1和2表示,0表示已经闭合 下面的蓝线是黄色格子的轮廓线,dp转移要把 ...
- BZOJ1814: Ural 1519 Formula 1(插头Dp)
Description Regardless of the fact, that Vologda could not get rights to hold the Winter Olympic gam ...
- 【Ural】1519. Formula 1 插头DP
[题目]1519. Formula 1 [题意]给定n*m个方格图,有一些障碍格,求非障碍格的哈密顿回路数量.n,m<=12. [算法]插头DP [题解]<基于连通性状态压缩的动态规划问题 ...
- 【BZOJ1814】Ural 1519 Formula 1 (插头dp)
[BZOJ1814]Ural 1519 Formula 1 (插头dp) 题面 BZOJ Vjudge 题解 戳这里 上面那个链接里面写的非常好啦. 然后说几个点吧. 首先是关于为什么只需要考虑三进制 ...
随机推荐
- Java数据持久层框架 MyBatis之API学习十(Logging详解)
对于MyBatis的学习而言,最好去MyBatis的官方文档:http://www.mybatis.org/mybatis-3/zh/index.html 对于语言的学习而言,马上上手去编程,多多练习 ...
- Abp.NHibernate连接PostgreSQl数据库
Abp.NHibernate动态库连接PostgreSQl数据库 初次接触Abp框架,其框架中封装的操作各类数据的方法还是很好用的,本人还在进一步的学习当中,并将利用abp.NHibernate类库操 ...
- 《Java编程的逻辑》终于上市了!
2018年1月下旬,<Java编程的逻辑>终于出版上市了! 这是老马过去两年死磕到底.无数心血的结晶啊! 感谢"博客园"的广大读者们,你们对老马文章的极高评价.溢美之词 ...
- Storm集群安装与部署
准备 1.三台虚拟机 192.168.1.128 Nimbus 192.168.1.131 Supervisor 192.168.1.132 Supervisor 2.JDK1.8 3.Zookeep ...
- lambda高级进阶--延迟执行
前面的整理我们也已经说到了,使用lambda表达式的主要原因是,将代码的执行延迟到一个合适的时间点.在使用lambda表达式的时候务必记住一点就是说lambda表达式都是延迟执行的. 延迟执行代码的原 ...
- 【Spring实战】--1Spring的核心
最近面试总会涉及Spring的优点,SpringMVC与Struts2的比较,生活慢慢稳定下来,这些面试还是应了那句话“只顾盲目拉车,不会低头看路”,回过头来还是要好好研究一下Spring,如果仅仅是 ...
- Hibernate (四)
1 懒加载 (感觉用处不是很大) 需要的时候加载,不需要的时候就不加载. 1.1 环境的搭建 hibernate.cfg.xml <?xml version='1.0' encoding='ut ...
- TP手册学习第一天
调试执行的sql语句 User::get(1); echo User::getLastSql(); 方法直接返回当前的查询SQL而不执行fetchSql echo User::fetchSql()-& ...
- Linux系统中常用操作命令
常用指令 ls 显示文件或目录 -l 列出文件详细信息l(list) -a 列出当前目录下所有文件及目录,包括隐藏的a(all)mkdi ...
- Mysql高可用架构(主从同步)
做高可用的优势 1.成本低 2.解决单点故障 3.不容易遇到性能瓶颈 一 .Mysql主从同步架构搭建案例 优点如下:·在业务繁忙阶段,在从服务器上可以执行查询工作(即我们常说的读写分离),降低主服务 ...