URAL1519:Formula 1——题解
http://acm.timus.ru/problem.aspx?space=1&num=1519
https://vjudge.net/problem/URAL-1519
题目大意:给一个网格,有些网格有障碍,问有多少条哈密顿回路。
——————————————————-
插头dp模板题,详见:http://www.cnblogs.com/luyouqi233/p/8256778.html
参考代码和思路于:http://www.yhzq-blog.cc/%E6%8F%92%E5%A4%B4dp-%E4%BB%8E%E4%B8%8D%E4%BC%9A%E5%88%B0%E5%B4%A9%E6%BA%83/
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const int INF=;
const int mod=;
const int M=;
struct node{
int to,nxt;
}edge[M];
int head[M],cnt;
int n,m,e1,e2;
bool mp[][]; //记录是否有障碍
int cur,pre; //滚动数组
int state[][M]; //记录状态,滚动
ll ans[][M],cntt; //记录答案
int tot[]; //记录状态总数
int bit[]; //提取状态使用
inline void getbit(){
for(int i=;i<;i++)bit[i]=i<<;
return;
}
inline void add(int u,int v){
cnt++;
edge[cnt].to=v;
edge[cnt].nxt=head[u];
head[u]=cnt;
return;
}
void insert(int now,ll num){
int u=now%mod;
for(int i=head[u];i;i=edge[i].nxt){
int v=edge[i].to;
if(state[cur][v]==now){
ans[cur][v]+=num;
return;
}
}
add(u,++tot[cur]);
state[cur][tot[cur]]=now;
ans[cur][tot[cur]]=num;
return;
}
void plugdp(){
cur=;
tot[cur]=;
ans[cur][]=;
state[cur][]=;
for(int i=;i<=n;i++){
for(int j=;j<=tot[cur];j++){
state[cur][j]<<=;
}
for(int j=;j<=m;j++){
memset(head,,sizeof(head));cnt=;
pre=cur,cur^=;
tot[cur]=;
for(int k=;k<=tot[pre];k++){
int now=state[pre][k];
ll num=ans[pre][k];
int is_down=(now>>bit[j-])%;
int is_right=(now>>bit[j])%;
if(!mp[i][j]){
if(!is_down&&!is_right)
insert(now,num);
}
else if(!is_down&&!is_right){
if(mp[i][j+]&&mp[i+][j])
insert(now+(<<bit[j-])+*(<<bit[j]),num);
}
else if(!is_down&&is_right){
if(mp[i][j+])
insert(now,num);
if(mp[i+][j])
insert(now-is_right*(<<bit[j])+is_right*(<<bit[j-]),num);
}
else if(is_down&&!is_right){
if(mp[i+][j])
insert(now,num);
if(mp[i][j+])
insert(now-is_down*(<<bit[j-])+is_down*(<<bit[j]),num);
}
else if(is_down==&&is_right==){
int count=;
for(int l=j+;l<=m;l++){
if((now>>bit[l])%==)count++;
if((now>>bit[l])%==)count--;
if(!count){
insert(now-(<<bit[l])-(<<bit[j-])-(<<bit[j]),num);
break;
}
}
}
else if(is_down==&&is_right==){
int count=;
for (int l=j-;l>=;l--){
if((now>>bit[l])%==) count--;
if((now>>bit[l])%==) count++;
if(!count){
insert(now+(<<bit[l])-*(<<bit[j-])-*(<<bit[j]),num);
break;
}
}
}
else if(is_down==&&is_right==)
insert(now-*(<<bit[j-])-(<<bit[j]),num);
else if(is_down==&&is_right==){
if(i==e1&&j==e2)
cntt+=num;
}
}
}
}
return;
}
int main(){
getbit();
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
char ch=getchar();
while(ch!='*'&&ch!='.')ch=getchar();
if(ch=='.')mp[i][j]=,e1=i,e2=j;
}
}
plugdp();
printf("%lld\n",cntt);
return ;
}
URAL1519:Formula 1——题解的更多相关文章
- URAL1519 Formula 1 —— 插头DP
题目链接:https://vjudge.net/problem/URAL-1519 1519. Formula 1 Time limit: 1.0 secondMemory limit: 64 MB ...
- URAL1519 Formula 1 【插头dp】
题目链接 URAL1519 题解 看题型显然插头\(dp\) 考虑如何设计状态 有这样一个方案 当我们决策到某个位置 轮廓线长这样 你会发现插头一定是相互匹配的 所以我们实际上可以把状态用括号序列表示 ...
- 2019.01.23 ural1519 Formula 1(轮廓线dp)
传送门 轮廓线dpdpdp模板题. 题意简述:给一个放有障碍的网格图,问有多少种方法能使所有非障碍格子都在同一条哈密顿回路上面. 考虑用括号序列的写法来状压这个轮廓线. 用000表示没有插头,111表 ...
- [URAL1519] Formula 1 [插头dp入门]
题面: 传送门 思路: 插头dp基础教程 先理解一下题意:实际上就是要你求这个棋盘中的哈密顿回路个数,障碍不能走 看到这个数据范围,还有回路处理,就想到使用插头dp来做了 观察一下发现,这道题因为都是 ...
- DP设状态 : 状压与线
[NOIP2017]宝藏(状压) [AHOI2009]中国象棋(状压) [BZOJ1814] URAL1519 Formula 1(插头\(DP\)模板) 新链接 : Luogu5056 , dark ...
- HDU1964 Pipes —— 插头DP
题目链接:https://vjudge.net/problem/HDU-1964 Pipes Time Limit: 5000/1000 MS (Java/Others) Memory Limi ...
- FZU1977 Pandora adventure —— 插头DP
题目链接:https://vjudge.net/problem/FZU-1977 Problem 1977 Pandora adventure Accept: 597 Submit: 2199 ...
- Gym 101480F Frightful Formula(待定系数)题解
#include<cmath> #include<set> #include<map> #include<queue> #include<cstd ...
- Codeforces Gym 100610 Problem E. Explicit Formula 水题
Problem E. Explicit Formula Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...
随机推荐
- Elasticsearch5.x版本中对Text类型进行聚合时提示illegal_argument_exception
Having this field in my mapping "answer": { "type": "text", "fiel ...
- 【转】linux下,如何把整个文件夹上传到服务器(另一台linux)
原文转自:https://zhidao.baidu.com/question/1046040541327493019.html 1.Linux下目录复制:本机->远程服务器 scp -r /h ...
- Maven学习(七)-----Maven添加远程仓库
Maven添加远程仓库 默认情况下,Maven从Maven中央仓库下载所有依赖关系.但是,有些库丢失在中央存储库,只有在Java.net或JBoss的储存库远程仓库中能找到. 1. Java.net资 ...
- python-编程从入门到实践
python-编程从入门到实践 1.python文件后缀名: .py 是Python的源码文件,由Python.exe解释. .pyc 是Python的编译文件.pyc 文件往往代替 py 文件发布: ...
- Jmeter资源监控工具ServerAgent运行原理的一些研究
用过Jmeter的应该都了解,有个ServerAgent工具,放在linux或者windows服务器上开启服务后,在Jmeter中配置下监视器,就可以抓取到服务器的一些资源信息,抓取的主要是cpu.内 ...
- charles基本使用文档
Charles 主要的功能包括: 截取 Http 和 Https 网络封包. 支持重发网络请求,方便后端调试. 支持修改网络请求参数. 支持网络请求的截获并动态修改. 支持模拟慢速网络. Charle ...
- Windows下Mongodb安装部署
1.下载安装包 mongodb-win32-x86_64-enterprise-windows-64-3.6.4.zip 解压 安装失败(当前环境windows server2012 R2):已验证可 ...
- Unity3D之AR开发(二)
上一篇给大家介绍了高通AR的使用,接下来给大家分享一下EasyAR EasyAR引擎简介 EasyAR是做好用的且免费的增强现实(Augmented Reality)引擎,EasyAR为Unity开发 ...
- redis rdb aof比较
Redis中数据存储模式有2种:cache-only,persistence; cache-only即只做为“缓存”服务,不持久数据,数据在服务终止后将消失,此模式下也将不存在“数据恢复”的手段,是一 ...
- [C++] Solve "Launch Failed. Binary not found." error on Eclipse
This error is that the default lanch configuration is not being created for this project. To solve i ...