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. js正则表达式校验正数、负数、和小数:^(\-|\+)?\d+(\.\d+)?$

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. spring mvc 和spring security配置 web.xml设置

    <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmln ...

  3. 灾难 bzoj 2815

    灾难(1s 128MB)catas [样例输入] 5 0 1 0 1 0 2 3 0 2 0 [样例输出] 4 1 0 0 0 题解: 主要算法:拓扑排序:最近公共祖先(Lca): 先跑出拓扑序 我们 ...

  4. pandas.DataFrame对行和列求和及添加新行和列

    导入模块: from pandas import DataFrame import pandas as pd import numpy as np 生成DataFrame数据 df = DataFra ...

  5. 【转载】10 个实用技巧,让 Finder 带你飞

    来自:http://sspai.com/27403/ Finder 是 Mac 电脑的系统程序,有的功能类似 Windows 的资源管理器.它是我们打开 Mac 首先见到的「笑脸」,有了它,我们可以组 ...

  6. js模仿ios select效果

    github:https://github.com/zhoushengmufc/iosselect webapp模仿ios下拉菜单 html下拉菜单select在安卓和IOS下表现不一样,iossel ...

  7. no identity found Command /usr/bin/codesign failed with exit code 1 报错解决方法

    stackoverflow 的解决方法是 xcode->preference->account->view detail -> refresh the provisioning ...

  8. iOS - ViewController的生命周期

    iOS SDK中提供很多原生的ViewController,大大提高了我们的开发效率:那么下面我们就根据开发中我们常用的ViewController谈一谈它的生命周期: (一)按照结构和用法可以对iO ...

  9. iOS---关于UIWebView

    1.加载的web界面可以自由收缩 webView.scalesPageToFit = YES;

  10. 基于JQuery实现的文本框自动填充功能

    1. 实现的方法 /* * js实现的文本框的自动完成功能 */ function doAutoComplete(textid,dataid,url){ $("#" + texti ...