ural 1519 Formula 1(插头dp)
1519. Formula 1 @ Timus Online Judge
干了一天啊!!!插头DP入门。
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <ctime>
#include <cmath> using namespace std;
typedef long long LL; const int N = ;
const int M = ;
LL pw[N], dp[][M];
int stk[][M], top[];
char mat[N][N];
bool vis[M]; void PRE() {
pw[] = ;
for (int i = ; i < N; i++) pw[i] = pw[i - ] * ;
} inline int getst(int st, int k) { return st / pw[k] % ;}
inline void print(int st, int n) { for (int i = n; i >= ; i--) printf("%d", getst(st, i));} LL work(int n, int m) {
bool fi = true;
int cur = , ei, ej, nx, st, hi, lo;
memset(dp, , sizeof(dp));
memset(vis, , sizeof(vis));
memset(top, , sizeof(top));
for (int i = ; i < n; i++) for (int j = ; j < m; j++) if (mat[i][j] == '.') ei = i, ej = j;
for (int i = ; i < n; i++) {
for (int j = ; j < m; j++) {
/************* debug ****************/
//cout << i << ' ' << j << ' ' << top[cur] << endl;
//for (int k = 0; k < top[cur]; k++) {
//st = stk[cur][k];
//print(st, m); cout << "~" << dp[cur][st] << ' ';
//}
//cout << endl;
/************************************/
if (i == ei && j == ej) return dp[cur][pw[m] << | ];
for (int k = ; k < top[cur]; k++) vis[stk[cur][k]] = false;
cur ^= ;
for (int k = ; k < top[cur]; k++) dp[cur][stk[cur][k]] = ;
top[cur] = ;
if (mat[i][j] == '*') {
if (fi) continue;
for (int k = ; k < top[cur ^ ]; k++) {
st = stk[cur ^ ][k];
if (getst(st, m)) continue;
nx = st * ;
dp[cur][nx] += dp[cur ^ ][st];
if (!vis[nx]) vis[stk[cur][top[cur]++] = nx] = true;
}
} else {
if (fi) {
dp[cur][] = ;
vis[stk[cur][top[cur]++] = ] = true;
fi = false;
} else {
for (int k = ; k < top[cur ^ ]; k++) {
st = stk[cur ^ ][k];
lo = getst(st, ), hi = getst(st, m);
if (lo == ) {
if (hi == ) {
nx = st * + ;
dp[cur][nx] += dp[cur ^ ][st];
if (!vis[nx]) vis[stk[cur][top[cur]++] = nx] = true;
} else if (hi == ) {
if (i == || mat[i - ][j] == '*') continue;
nx = (st * + ) % pw[m + ];
dp[cur][nx] += dp[cur ^ ][st];
if (!vis[nx]) vis[stk[cur][top[cur]++] = nx] = true;
nx = (st * + ) % pw[m + ];
dp[cur][nx] += dp[cur ^ ][st];
if (!vis[nx]) vis[stk[cur][top[cur]++] = nx] = true;
} else {
if (i == || mat[i - ][j] == '*') continue;
nx = (st * + ) % pw[m + ];
dp[cur][nx] += dp[cur ^ ][st];
if (!vis[nx]) vis[stk[cur][top[cur]++] = nx] = true;
nx = (st * + ) % pw[m + ];
dp[cur][nx] += dp[cur ^ ][st];
if (!vis[nx]) vis[stk[cur][top[cur]++] = nx] = true;
}
} else if (lo == ) {
if (j == || mat[i][j - ] == '*') continue;
if (hi == ) {
nx = st * % pw[m + ];
dp[cur][nx] += dp[cur ^ ][st];
if (!vis[nx]) vis[stk[cur][top[cur]++] = nx] = true;
nx = (st * - ) % pw[m + ];
dp[cur][nx] += dp[cur ^ ][st];
if (!vis[nx]) vis[stk[cur][top[cur]++] = nx] = true;
} else if (hi == ) {
if (i == || mat[i - ][j] == '*') continue;
int cnt = , i = m - , t;
for ( ; i >= ; i--) {
t = getst(st, i);
if (t == ) cnt++;
if (t == ) {
if (cnt) cnt--;
else break;
}
}
nx = (st - pw[i] - ) * % pw[m + ];
//print(st, m); putchar(' '); print(nx, m); puts("");
dp[cur][nx] += dp[cur ^ ][st];
if (!vis[nx]) vis[stk[cur][top[cur]++] = nx] = true;
} else {}
} else {
if (j == || mat[i][j - ] == '*') continue;
if (hi == ) {
nx = st * % pw[m + ];
dp[cur][nx] += dp[cur ^ ][st];
if (!vis[nx]) vis[stk[cur][top[cur]++] = nx] = true;
nx = (st * - ) % pw[m + ];
dp[cur][nx] += dp[cur ^ ][st];
if (!vis[nx]) vis[stk[cur][top[cur]++] = nx] = true;
} else if (hi == ) {
if (i == || mat[i - ][j] == '*') continue;
nx = st / * % pw[m + ];
dp[cur][nx] += dp[cur ^ ][st];
if (!vis[nx]) vis[stk[cur][top[cur]++] = nx] = true;
} else {
if (i == || mat[i - ][j] == '*') continue;
int cnt = , i = , t;
for ( ; i <= m; i++) {
t = getst(st, i);
if (t == ) cnt++;
if (t == ) {
if (cnt) cnt--;
else break;
}
}
nx = (st + pw[i] - ) * % pw[m + ];
dp[cur][nx] += dp[cur ^ ][st];
if (!vis[nx]) vis[stk[cur][top[cur]++] = nx] = true;
}
}
}
}
}
}
}
return ;
} int main() {
//freopen("in", "r", stdin);
//freopen("out", "w", stdout);
//time_t t1 = clock();
int n, m;
PRE();
//cout << (double) (clock() - t1) / CLOCKS_PER_SEC << endl;
while (cin >> n >> m) {
for (int i = ; i < n; i++) { for (int j = ; j < m; j++) cin >> mat[i][j]; mat[i][m] = ;}
cout << work(n, m) << endl;
//cout << (double) (clock() - t1) / CLOCKS_PER_SEC << endl;
}
return ;
}
——written by Lyon
ural 1519 Formula 1(插头dp)的更多相关文章
- 【BZOJ1814】Ural 1519 Formula 1 插头DP
[BZOJ1814]Ural 1519 Formula 1 题意:一个 m * n 的棋盘,有的格子存在障碍,求经过所有非障碍格子的哈密顿回路个数.(n,m<=12) 题解:插头DP板子题,刷板 ...
- bzoj1814 Ural 1519 Formula 1(插头dp模板题)
1814: Ural 1519 Formula 1 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 924 Solved: 351[Submit][Sta ...
- bzoj 1814 Ural 1519 Formula 1 插头DP
1814: Ural 1519 Formula 1 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 942 Solved: 356[Submit][Sta ...
- bzoj 1814 Ural 1519 Formula 1 ——插头DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1814 普通的插头 DP .但是调了很久.注意如果合并两个 1 的话,不是 “把向右第一个 2 ...
- Ural 1519 Formula 1 插头DP
这是一道经典的插头DP单回路模板题. 用最小表示法来记录连通性,由于二进制的速度,考虑使用8进制. 1.当同时存在左.上插头的时候,需要判断两插头所在连通块是否相同,若相同,只能在最后一个非障碍点相连 ...
- BZOJ1814: Ural 1519 Formula 1(插头Dp)
Description Regardless of the fact, that Vologda could not get rights to hold the Winter Olympic gam ...
- bzoj 1814: Ural 1519 Formula 1 插头dp经典题
用的括号序列,听说比较快. 然并不会预处理,只会每回暴力找匹配的括号. #include<iostream> #include<cstdio> #include<cstr ...
- 【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多线程三种方式
java多线程都有几种方式 有三种: (1)继承Thread类,重写run函数 创建: class xx extends Thread{ public void run(){ Thread.sleep ...
- LintCode_514 Paint Fence
题目 here is a fence with n posts, each post can be painted with one of the k colors.You have to paint ...
- Spring线程安全的实现机制--ThreadLocal
转载: http://blog.csdn.net/lufeng20/article/details/24314381
- 项目中的那些事---Java反射的应用
最近工作中遇到一个这样的问题: 为某个项目中的所有接口做一个测试工具,使用java Swing技术,该项目有不同的版本,不是所有版本中的接口都是相同的,而我做的工具需要兼容所有版本. 于是就引入了这样 ...
- linux如何将分组权限置为空
两种方法 方法一:使用-符号 chmod g=- monkey.py#可以单独指定一个 方法二:简写方式,用0表示 chmod 740 monkey.py#必须同时指定三个的权限
- 提升mysql服务器性能(复制原理与拓扑优化)
原文:提升mysql服务器性能(复制原理与拓扑优化) 版权声明:皆为本人原创,复制必究 https://blog.csdn.net/m493096871/article/details/9008171 ...
- 哈哈哈哈,我竟然发现了个MSDN里面的笔误
typedef __PROCESSOR_INFO { WORD wVersion; WCHAR szProcessorCore[40]; WORD wCoreRevision; WCHAR ...
- ucore os 前初始化
BIOS 初始化完成说起 连接的时候指定了 -Ttext 0x7c00 也指定了 -e start 所以booasm.S 中的start 就呗钦定为程序入口了. 开始就是 屏蔽中断 初始化段寄存器 使 ...
- 发布Qt Widgets桌面应用程序的方法
Qt是一款优秀的跨平台开发框架,它可以在桌面.移动平台以及嵌入式平台上运行.目前Qt 5介绍程序发布的文章帖子比较少.大家又非常想要知道如何发布Qt应用程序,于是我花了一点儿时间介绍一下如何发布Qt桌 ...
- 第十章—DOM(0)—NODE类型
DOM1定义了一个node接口,该接口由DOM的所有节点类型实现. 所有的节点都存在这样或那样的关系.在HTML中,head,body可以看出是html的子元素,html是head,body的父元素, ...