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. Mac安装软件新方法:Homebrew-cask

    Homebrew是Ruby社区极富想象力的一个作品,使得Mac下安装Mysql等常用包不再困难.那么,是否也可以通过brew install mysql这样简单的方式来安装chrome浏览器? 近期, ...

  2. 外观模式(Facade)(门面模式、子系统容易使用)

    外观(Facade)模式的定义:是一种通过为多个复杂的子系统提供一个一致的接口,而使这些子系统更加容易被访问的模式.该模式对外有一个统一接口,外部应用程序不用关心内部子系统的具体的细节,这样会大大降低 ...

  3. Git.之.最小化配置

    Git.之.最小化配置 做一个全局的用户配置,便于以后提交代码等,记录当前操作的用户. ## 添加配置 # git config [--local | --global | --system] use ...

  4. 威胁快报|Nexus Repository Manager 3新漏洞已被用于挖矿木马传播,建议用户尽快修复

    背景 近日,阿里云安全监测到watchbog挖矿木马使用新曝光的Nexus Repository Manager 3远程代码执行漏洞(CVE-2019-7238)进行攻击并挖矿的事件. 值得注意的是, ...

  5. JavaScript--jquery.min.js文件

    /*! jQuery v1.12.3 | (c) jQuery Foundation | jquery.org/license */ !function(a,b){"object" ...

  6. 2018 8.8 提高A组模拟赛

    T1 Description 被污染的灰灰草原上有羊和狼.有N只动物围成一圈,每只动物是羊或狼. 该游戏从其中的一只动物开始,报出[1,K]区间的整数,若上一只动物报出的数是x,下一只动物可以报[x+ ...

  7. notepad++ 退出后关闭所有文档(关闭“记住最后打开的文件”)

    旧版本: 设置->首选项->其他->取消勾选Remember current session for next launch 新版本: 设置->首选项->备份->取 ...

  8. 【JZOJ4929】【NOIP2017提高组模拟12.18】B

    题目描述 在两个n*m的网格上染色,每个网格中被染色的格子必须是一个四联通块(没有任何格子被染色也可以),四联通块是指所有染了色的格子可以通过网格的边联通,现在给出哪些格子在两个网格上都被染色了,保证 ...

  9. ELK背景介绍1

    一.elasticsearch背景介绍 1.问题引入:搜索所有天安门相关的内容,大数据量的判断,加索引orm,歌词怎么做?等等问题,大公司上亿条数据怎样开发处理日志? 2.ELK框架,目前先学习E(e ...

  10. 【Java-加密算法】对称加密、非对称加密、单向散列(转)

    一提到加密,就会联想到数字签名,这两个经常被混淆的概念到底是什么呢? 加密:加密是一种以密码方式发送信息的方法.只有拥有正确密钥的人才能解开这个信息的密码.对于其他人来说,这个信息看起来就像是一系列随 ...