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. Hdu 1384(差分约束)

    题目链接 Intervals Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  2. 小希的迷宫 HDU - 1272 (并查集)

    思路: 当图中的集合(连通子图)个数为1并且边数等于顶点数-1(即改图恰好为一棵生成树)时,输出Yes. 此题的坑:(1) 如果只输入0 0算作一组数据的话答案应该输出Yes (2) 输入数据可能并不 ...

  3. 洛谷P4145 上帝造题的七分钟2 / 花神游历各国(重题:洛谷SP2713 GSS4 - Can you answer these queries IV)

    题目背景 XLk觉得<上帝造题的七分钟>不太过瘾,于是有了第二部. 题目描述 "第一分钟,X说,要有数列,于是便给定了一个正整数数列. 第二分钟,L说,要能修改,于是便有了对一段 ...

  4. GYM100741 A Queries

    A. Queries time limit per test 0.25 s memory limit per test 64 MB input standard input output standa ...

  5. poj 2001 Shortest Prefixes(字典树trie 动态分配内存)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15610   Accepted: 673 ...

  6. ASP.NET 自定义服务器控件

    文章内容   本文通过创建一个最简单的服务器控件,演示开发服务器端控件的流程. 文章内容整理自MSDN的编程指南,原文地址在文章末尾的资源中. 本文创建一个简单的服务器控件,名为 RedLabel.  ...

  7. Django框架Day1------之url和views

    一.新建一个Django程序(window 7进入cmd里面操作):注意,此处要需在指定的文件夹下 a,django-admin startproject django_test(django_tes ...

  8. 怎么在 CentOS 6 上配置私有 NPM 仓库?

    Sinopia 是一个简单易用的私有 NPM 仓库服务器.在 CentOS 6 上安装时,遇到如下报错(Node 版本 6.9.1) #error This version of node/NAN/v ...

  9. C++不支持默认的int

    VS: 工程属性->C/C++->命令行->输入 /wd4430

  10. iOS GCD 使用

    1. dispatch queue的概念 dispatch queue分成以下三种: a)运行在主线程的Main queue,通过dispatch_get_main_queue获取.dispatch_ ...