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 ...
随机推荐
- 将 Python3 文件打包成 exe 文件
我们用 Python 写好的代码,如何给别人在没有配置 Python 环境的情况下直接使用呢?尤其是面向 windows 众. 因为 Python 是一门解释性的语言,离开了 Python 解释器,P ...
- WPF RegisterAttached ListBoxItem(附加属性传递到Item)
/// <summary> /// Controls的附加属性 /// </summary> public class ControlsAttached : Dependenc ...
- Maven学习(十六)-----Maven存储库
什么是Maven资源库? 在 Maven 术语里存储库是一个目录,即目录中保存所有项目的 jar 库,插件或任何其他项目特定文件,并可以容易由 Maven 使用. Maven库中有三种类型 local ...
- web存储机制(localStorage和sessionStorage)
web存储包括两种:sessionStorage 和 localStorage(都是限定在文档源级别,非同源文档间无法共享) 1.sessionStorage 数据放在服务器上(IE不支持) 严格用于 ...
- qt linux系统下出现Qt5: Unknown module(s) in QT: serialport问题解决
需要单独安装这个模块, manjaro linux打开包管理器,搜索安装,就好了
- python-创建进程的三种方式
目录 1,os.fork() 方法 2,Process方法 3,Pool方法 1,os.fork() 方法 import os ret = os.fork() if ret == 0: #子进程 pr ...
- pyextend库-unpack列表集合字符串解包函数
pyextend - python extend lib unpack (iterable, count, fill=None) 参数: iterable: 实现 __iter__的可迭代对象, 如 ...
- Scrum立会报告+燃尽图(十月二十六日总第十七次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2246 项目地址:https://git.coding.net/zhang ...
- 【Alpha】阶段第二次Scrum Meeting
[Alpha]阶段第二次Scrum Meeting 工作情况 团队成员 今日已完成任务 明日待完成任务 刘峻辰 发表评论接口 更新评论接口 赵智源 部署实际项目 编写脚本实现持续集成 肖萌威 编写注册 ...
- c# WPS DLL及其调用
1.dll分享(含xsl及docx的dll) 链接:https://pan.baidu.com/s/1c1ImV14OndmvIb4W-_WL2A 密码:d2rx 2.方法: 1.先在类的前面(类外面 ...