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 ...
随机推荐
- HTML基本代码教学,第三天
HTML 今天由于个人情况,身体不适,但是为了大家的学习进度,咱们以纯文字得形式来简单了解下今天的学习内容 今儿咱们来了解下表单 <form id=" " name=&qu ...
- [.NET] 使用HttpClient操作HFS (HTTP File Server)
前言 本篇文章介绍如何使用HttpClient操作HFS (HTTP File Server),为自己留个纪录也希望能帮助到有需要的开发人员.关于HTTP File Server的介绍.安装.设定,可 ...
- php实现图形计算器
存档: index.php <html> <head> <title>图形计算器开发</title> <meta http-equiv=" ...
- window + office 激活方法(不提供下载)
下载KMSTools工具,里面集成了许多激活方法. WINDOWS KMS激活方式 比较简单,随便点一个KMSTools带有KMS字样的工具就可以了,但是KMS激活方式有效期为180天,到期需要继续激 ...
- HTML5 + CSS3 实现地球绕太阳公转
使用的是正面视角,主要是用 HTML5 + CSS3 来实现,JS只是用来画图. test.html: <!DOCTYPE html> <html> <head> ...
- Linux 安装ActiveMQ(使用Mac远程访问)
阅读本文需要安装JDK 一 ActiveMQ简介 activemq是用java语言编写的一款开源消息总线 activemq是apache出品 activemq消息的传递有两种类型 一种是点对点: 即一 ...
- AtCoder Grand Contest 026 D - Histogram Coloring
一列中有两个连续的元素,那么下一列只能选择选择正好相反的填色方案(因为连续的地方填色方案已经确定,其他地方也就确定了) 我们现将高度进行离散化到Has数组中,然后定义dp数组 dp[i][j] 表示前 ...
- Maven私库
<server> <id>releases</id> <username>admin</username> <password> ...
- 剑指offer-二维数组中的查找01
题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数 ...
- zookeeper的选举过程
zookeeper的选举过程大致如下: zookeeper的选举过程,就是选出一个在n/2+1个节点中选出一个节点为主节点的过程.比如,当我们启动一个有5个节点的zookeeper集群的时候.首先启动 ...