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 ...
随机推荐
- WebAPI学习笔记
WebAPI WebApi是添加到Asp.Net平台的一个新特性,可以快速的创建Web服务,并对客户端提供HTTP的API调用接口 WebApi是建立在MVC框架基础之上,但不属于MVC的一部分. 序 ...
- 180730-Spring之RequestBody的使用姿势小结
Spring之RequestBody的使用姿势小结 SpringMVC中处理请求参数有好几种不同的方式,如我们常见的下面几种 根据 HttpServletRequest 对象获取 根据 @PathVa ...
- git remote add origin错误
如果输入$ Git remote add origin git@github.com:djqiang(github帐号名)/gitdemo(项目名).git 提示出错信息:fatal: remote ...
- 使用JS验证文件类型
项目中涉及到这一需求,在此贴出代码分享给大家, 有2中方式,一种是input中使用accept 方式 一种是使用js正则表达式判断,个人推荐使用js正则表达式,因为accept 有的浏览器并不支持,而 ...
- [寒假学习笔记](一)Markdown语法学习
Markdown 学习 在博客园上使用markdown编辑,记录学习进度,以来日可以复习 前期准备 1. 安装markdownpad2 官网直接找下载安装,遇到bug他会自动提示信息,跟着提示去安装一 ...
- Visual Studio 调试时无法命中断点
1.查看代码优化是否勾选,如有去掉勾选 2.确保是在Debug模式下设置的断点 3.确保在启动时未修改代码即“要求源文件和原始版本完全匹配” 4.DLL的引用问题
- springboot 集成 swagger
1. 首先配置swaggerConfigpackage com.lixcx.lismservice.config; import com.lixcx.lismservice.format.Custom ...
- Scrum立会报告+燃尽图(十月二十八日总第十九次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2288 项目地址:https://git.coding.net/zhang ...
- NumPy常用函数总结
转载:https://www.cnblogs.com/hd-chenwei/p/6832732.html NumPy库总包含两种基本的数据类型:矩阵和数组,矩阵的使用类似Matlab,本实例用得多的是 ...
- DWZ-JUI+UEditor第二次不显示,UEditor异步加载第二次不显示的解决方案
使用UEditor-1.4.3中遇到第一次跳转到使用UEditor的界面后,编辑器加载正常,返回后第二次再跳转到这个界面就出现UEditor无法正常加载, 也没百度到答案,看UEditor源码,发现这 ...