http://acm.timus.ru/problem.aspx?space=1&num=1519

调了好久啊。参考(抄)的iwtwiioi的题解

如果想要题解,题解在《基于连通性状态压缩的动态规划问题》。

我肯定讲得不如题解清楚QAQ,所以还是不讲了(╯‵□′)╯︵┻━┻

连通性状压得用bfs扩展同时hash表判重。为什么呢?

因为状态太多数组存不下,只能用hash表存有用的状态,没错就是这样!!!

感觉代码量比普通状压高了不少QAQ

这么复杂,还是典型例题→_→。

时间复杂度\(O(n*m^22^{(m+1)*2})\)。

我用的四进制表示状态,常数比三进制小。bfs和hash表应该能跑得比理论复杂度快得多吧。

#include<cstdio>
#include<cstring>
#include<algorithm>
#define BIT(a, b) ((a) << ((b) << 1))
#define CLR(a, b) (a ^= ((a) & BIT(3, b)))
#define GET(a, b) (((a) >> ((b) << 1)) & 3)
using namespace std;
const int M = 1000007;
typedef long long ll; bool mp[12][12], flag; ll ans = 0; int n, m, lan = 0, lam = 0; struct node {
int to[M], hash[M], cnt;
ll sum[M]; node() {
memset(to, 0, sizeof(to));
memset(hash, -1, sizeof(hash));
memset(sum, 0, sizeof(sum));
cnt = 0;
} void ins(int s, ll num) {
int pos = s % M; flag = false;
while (true) {
if (hash[pos] == -1) {hash[pos] = s; flag = true; break;}
if (hash[pos] == s) break;
++pos; if (pos == M) pos = 0;
}
sum[pos] += num;
if (flag) to[++cnt] = pos;
} void clr() {while (cnt) {hash[to[cnt]] = -1; sum[to[cnt]] = 0; --cnt;}}
} T1, T2; int cc = 0, nn; int find(int s, int tmp, int d) {
if (d) {
for (int i = tmp; i < m; ++i) {
nn = GET(s, i);
if (nn == 1) ++cc;
if (nn == 2) --cc;
if (cc == 0) return i;
}
} else {
for (int i = tmp; i >= 0; --i) {
nn = GET(s, i);
if (nn == 2) ++cc;
if (nn == 1) --cc;
if (cc == 0) return i;
}
}
} int l, u, d, r, pos; bool getnxt(int s, int row, int col, bool U, bool D, bool L, bool R, int &t, ll sum) {
if ((row == 0 && U) || (col == 0 && L) || (row == n - 1 && D) || (col == m - 1 && R)) return false;
if ((mp[row + 1][col] && D) || (mp[row][col + 1] && R)) return false;
if (row == lan && col == lam && (R || D)) return false;
l = GET(s, col); u = GET(s, col + 1);
if ((((bool) u) != U) || (((bool) l) != L)) return false;
t = s; CLR(t, col + 1); CLR(t, col); d = r = 0;
if (l == 0 && u == 0) {
if (D && R)
d = 1, r = 2;
} else if (l && u) {
if (l == 1 && u == 2) {
if (row != lan || col != lam) return false;
ans += sum;
} else if (l == 1 && u == 1) {
pos = find(s, col + 1, 1);
CLR(t, pos);
t |= BIT(1, pos);
} else if (l == 2 && u == 2) {
pos = find(s, col, 0);
CLR(t, pos);
t |= BIT(2, pos);
}
} else if (l && !u) {
if (D) d = l, r = 0;
if (R) r = l, d = 0;
} else if (!l && u) {
if (D) d = u, r = 0;
if (R) r = u, d = 0;
} t |= BIT(d, col); t |= BIT(r, col + 1);
if (col == m - 1) t <<= 2;
return true;
} void bfs() {
node *q1, *q2; ll sum; int s, t;
q1 = &T1; q2 = &T2;
q1->ins(0, 1);
for (int row = 0; row < n; ++row)
for (int col = 0; col < m; ++col) {
q2->clr();
for (int i = q1->cnt; i >= 1; --i) {
sum = q1->sum[q1->to[i]];
s = q1->hash[q1->to[i]];
if (mp[row][col]) {
if (getnxt(s, row, col, 0, 0, 0, 0, t, sum))
q2->ins(t, sum);
} else {
if (getnxt(s, row, col, 1, 1, 0, 0, t, sum)) q2->ins(t, sum);
if (getnxt(s, row, col, 1, 0, 1, 0, t, sum)) q2->ins(t, sum);
if (getnxt(s, row, col, 1, 0, 0, 1, t, sum)) q2->ins(t, sum);
if (getnxt(s, row, col, 0, 1, 1, 0, t, sum)) q2->ins(t, sum);
if (getnxt(s, row, col, 0, 1, 0, 1, t, sum)) q2->ins(t, sum);
if (getnxt(s, row, col, 0, 0, 1, 1, t, sum)) q2->ins(t, sum);
}
}
swap(q1, q2);
if (row == lan && col == lam) return;
}
} int main() {
scanf("%d%d", &n, &m);
char c;
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j) {
for (c = getchar(); c != '*' && c != '.'; c = getchar());
if (c == '*') mp[i][j] = true;
else lan = i, lam = j;
} bfs();
printf("%lld\n", ans);
return 0;
}

【URAL 1519】Formula 1的更多相关文章

  1. 【Ural】【1519】Formula 1

    插头DP 本题为CDQ<基于连通性状态压缩的动态规划的……(我忘了)>里的例题!(嗯就是这样……) 先膜拜一下ccy大神……http://blog.sina.com.cn/s/blog_5 ...

  2. 【hihocoder 1519】 逃离迷宫II

    [题目链接]:http://hihocoder.com/problemset/problem/1519?sid=1098756 [题意] Chinese [题解] bfs题; 根据bfs的性质; 第一 ...

  3. 【URAL 1018】Binary Apple Tree

    http://vjudge.net/problem/17662 loli蜜汁(面向高一)树形dp水题 #include<cstdio> #include<cstring> #i ...

  4. 【URAL 1486】Equal Squares

    题意:求给定字符矩阵中相同正方形矩阵的最大边长和这两个相同正方形的位置 第一次写字符串哈希,选两个不同的模数进行二维字符串哈希. 本来应该取模判断相等后再暴力扫矩阵来判断,但是我看到<Hash在 ...

  5. 【URAL 1297】Palindrome 最长回文子串

    模板题,,,模板打错查了1h+QAQ #include<cmath> #include<cstdio> #include<cstring> #include< ...

  6. 【URAL 1917】Titan Ruins: Deadly Accuracy(DP)

    题目 #include<cstdio> #include<algorithm> using namespace std; #define N 1005 int n, m, cn ...

  7. 【URAL 1486】Equal Squares(二维哈希+二分)

    Description During a discussion of problems at the Petrozavodsk Training Camp, Vova and Sasha argued ...

  8. 【URAL 1989】 Subpalindromes(线段树维护哈希)

    Description You have a string and queries of two types: replace i'th character of the string by char ...

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

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

随机推荐

  1. Memcached初识

    高性能分布式内存对象缓存系统. 参考: Memcached官网: Memcached简介-1:Memcached简介-2: Memcached 教程 | 菜鸟教程:

  2. bodyparser

    今天在用bodyparser时,遇到了如下问题: 首先科普下bodyparser的作用:它用于解析客户端请求的body中的内容,内部使用JSON编码处理,url编码处理以及对于文件的上传处理. 现在继 ...

  3. 为什么applicationContext.xml和spring-servlet.xml中都有注解过滤<context:component-scan base-package="myproject"> 和<context:component-scan base-package="myproject.controller" />

    在刚学习SpringMVC框架整合时,你也许会产生疑问为什么Spring.xml和SpringMVC.xml中都有注解过滤. <context:component-scan base-packa ...

  4. 微信小程序社区上线

    微信小程序公测了! 从首次得到微信小程序发布的消息开始,小木和Michael就进入了紧急备战状态. 除了要快速学通微信小程序开发之外,我们还做了这些工作: 1.录制全球首套微信小程序实战项目类视频教程 ...

  5. 【原】Go语言及Web框架Beego环境无脑搭建

    本文涉及软件均以截至到2013年10月12日的最新版本为准 1. 相关软件准备: 1) go1.2rc1.windows-386.msi,对应32位windows系统安装使用 下载地址: https: ...

  6. 微信小程序之基础简介

    创建小程序项目后进入编辑环境中会有以下的初始配置文件: 文件夹: 1.pages(存放小程序的页面) 1.index 2.logs (页面里的js文件 以Page()方法开头 所有参数对象都存放在其里 ...

  7. 关于多个block问题

    在某个添加文本的页面中,leftbarbutton是删除(直接将数组中的这个string删除),rightbarbutton是完成,分别对应两个block,完成的block是一开始写的,写到了view ...

  8. Android GZIP压缩IO流,优化APP数据传输(一)

    我是小搬运工,今天讲解流的压缩传输 在我们安卓开发中,通常伴随着大量的数据传输,在此,楼主给出一个简单的压缩流的数据进行操作. public void GZIPCpf(String path) { / ...

  9. 做一个 App 前需要考虑的几件事

    做一个 App 前需要考虑的几件事  来源:limboy的博客   随着工具链的完善,语言的升级以及各种优质教程的涌现,做一个 App 的成本也越来越低了.尽管如此,有些事情最好前期就做起来,避免当 ...

  10. Razor速记

    1.语法 @{     int c=2; } @for(int i=0;i<c;i++) {     @i     @:@i     @:select     @:select @i     s ...