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)的更多相关文章

  1. 【BZOJ1814】Ural 1519 Formula 1 插头DP

    [BZOJ1814]Ural 1519 Formula 1 题意:一个 m * n 的棋盘,有的格子存在障碍,求经过所有非障碍格子的哈密顿回路个数.(n,m<=12) 题解:插头DP板子题,刷板 ...

  2. bzoj1814 Ural 1519 Formula 1(插头dp模板题)

    1814: Ural 1519 Formula 1 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 924  Solved: 351[Submit][Sta ...

  3. 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 ...

  4. bzoj 1814 Ural 1519 Formula 1 ——插头DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1814 普通的插头 DP .但是调了很久.注意如果合并两个 1 的话,不是 “把向右第一个 2 ...

  5. Ural 1519 Formula 1 插头DP

    这是一道经典的插头DP单回路模板题. 用最小表示法来记录连通性,由于二进制的速度,考虑使用8进制. 1.当同时存在左.上插头的时候,需要判断两插头所在连通块是否相同,若相同,只能在最后一个非障碍点相连 ...

  6. BZOJ1814: Ural 1519 Formula 1(插头Dp)

    Description Regardless of the fact, that Vologda could not get rights to hold the Winter Olympic gam ...

  7. bzoj 1814: Ural 1519 Formula 1 插头dp经典题

    用的括号序列,听说比较快. 然并不会预处理,只会每回暴力找匹配的括号. #include<iostream> #include<cstdio> #include<cstr ...

  8. 【Ural】1519. Formula 1 插头DP

    [题目]1519. Formula 1 [题意]给定n*m个方格图,有一些障碍格,求非障碍格的哈密顿回路数量.n,m<=12. [算法]插头DP [题解]<基于连通性状态压缩的动态规划问题 ...

  9. 【BZOJ1814】Ural 1519 Formula 1 (插头dp)

    [BZOJ1814]Ural 1519 Formula 1 (插头dp) 题面 BZOJ Vjudge 题解 戳这里 上面那个链接里面写的非常好啦. 然后说几个点吧. 首先是关于为什么只需要考虑三进制 ...

随机推荐

  1. hdoj 1325 Is It A Tree? 【并查集】

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/shengweisong/article/details/34099151 做了一上午,最终ac了 w ...

  2. 提升mysql服务器性能(HA MMM MHA MaxScale)

    原文:提升mysql服务器性能(HA MMM MHA MaxScale) 版权声明:皆为本人原创,复制必究 https://blog.csdn.net/m493096871/article/detai ...

  3. python基础--基础数据类型

    1.输入 python2中的输入: 关键字:input()  --> 需要人为的告诉input你输入的是哪种类型的数据(声明数据类型) >>> name = input('pl ...

  4. Java IO:如何得到Jar包中内嵌Jar包的时间戳

    ClassLoader bladeClassLoader = BladeCLI.class.getClassLoader(); URL url = bladeClassLoader.getResour ...

  5. 在MaxCompute中利用bitmap进行数据处理

    很多数据开发者使用bitmap技术对用户数据进行编码和压缩,然后利用bitmap的与/或/非的极速处理速度,实现类似用户画像标签的人群筛选.运营分析的7日活跃等分析.本文给出了一个使用MaxCompu ...

  6. WCF 无管理员权限下启用服务

    1 使用 netsh.exe 工具 C:\Windows\system32>netsh http add urlacl url=http://+:8733/WcfServiceLibrary1 ...

  7. Google搜索技巧-入门篇

    基本搜索 Google 查询简洁方便,仅需输入查询内容并敲一下回车键 (Enter),或单击“Google 搜索”按钮即可得到相关资料. 搜索两个及两个以上关键字 Google 只会返回那些符合您的全 ...

  8. Element UI table组件源码分析

    本文章从如下图所示的最基本的table入手,分析table组件源代码.本人已经对table组件原来的源码进行削减,源码点击这里下载.本文只对重要的代码片段进行讲解,推荐下载代码把项目运行起来,跟着文章 ...

  9. ie8或9下ajax跨域问题

    ie8或9下ajax跨域支持,添加如下代码 <!--[if (IE 8)|(IE 9)]><script src="https://cdn.bootcss.com/jque ...

  10. python正则表达式应用 定义一个函数,求字符串中出现的所有整数之和