[BZOJ]1814 Ural 1519 Formula 1 插头DP

来源:互联网 发布:练字钢笔推荐 知乎 编辑:程序博客网 时间:2024/05/30 04:49

1814: Ural 1519 Formula 1

Time Limit: 1 Sec  Memory Limit: 64 MB
Submit: 879  Solved: 328
[Submit][Status][Discuss]

Description

Regardless of the fact, that Vologda could not get rights to hold the Winter Olympic games of 20**, it is well-known, that the city will conduct one of the Formula 1 events. Surely, for such an important thing a new race circuit should be built as well as hotels, restaurants, international airport - everything for Formula 1 fans, who will flood the city soon. But when all the hotels and a half of the restaurants were built, it appeared, that at the site for the future circuit a lot of gophers lived in their holes. Since we like animals very much, ecologists will never allow to build the race circuit over the holes. So now the mayor is sitting sadly in his office and looking at the map of the circuit with all the holes plotted on it. Problem Who will be smart enough to draw a plan of the circuit and keep the city from inevitable disgrace? Of course, only true professionals - battle-hardened programmers from the first team of local technical university!.. But our heroes were not looking for easy life and set much more difficult problem: "Certainly, our mayor will be glad, if we find how many ways of building the circuit are there!" - they said. It should be said, that the circuit in Vologda is going to be rather simple. It will be a rectangle N*M cells in size with a single circuit segment built through each cell. Each segment should be parallel to one of rectangle's sides, so only right-angled bends may be on the circuit. At the picture below two samples are given for N = M = 4 (gray squares mean gopher holes, and the bold black line means the race circuit). There are no other ways to build the circuit here. 一个 m * n 的棋盘,有的格子存在障碍,求经过所有非障碍格子的哈密顿回路个数

Input

The first line contains the integer numbers N and M (2 ≤ N, M ≤ 12). Each of the next N lines contains M characters, which are the corresponding cells of the rectangle. Character "." (full stop) means a cell, where a segment of the race circuit should be built, and character "*" (asterisk) - a cell, where a gopher hole is located.

Output

You should output the desired number of ways. It is guaranteed, that it does not exceed 2^63-1.

Sample Input

4 4
**..
....
....
....



Sample Output


2

HINT

Source

[Submit][Status][Discuss]


HOME Back

  插头DP比起其他DP还真的是骨(dai)骼(ma)精(ju)奇(chang)啊... 不过能想到插头这样一个很好的比喻还是非常6. cdq的讲稿非常清晰, 可惜万恶的百度文库需要1下载券... 搞得我花掉了我(b)自(f)己(k)的一个下载券... 这里附上一个同样清晰明了的讲解: 插头DP——从不会到崩溃. 说着崩溃讲的还是很好的嘛~ 但是确实恶心. 省选考到我就gg(flag). 这道题就是裸题啦~

#include<bits/stdc++.h>#define clear(a) memset(a, 0, sizeof(a))using namespace std;typedef long long lnt;const int maxn = 2e5 + 5;char s[15];lnt ans, dp[2][maxn];int n, m, c, cnt, ex, ey, tot[2], bit[20];int mp[15][15], h[maxn], sta[2][maxn];struct edge{int nxt, v;}e[maxn];inline void insert(const int &s, const lnt &num) {int u = s % maxn;for (int i = h[u]; i; i = e[i].nxt)if (sta[c][e[i].v] == s) {dp[c][e[i].v] += num;return;}e[++ cnt].v = ++ tot[c];e[cnt].nxt = h[u], h[u] = cnt;sta[c][tot[c]] = s, dp[c][tot[c]] = num;}inline void Plug_Dp() {tot[0] = 1, dp[c][1] = 1;register int i, j, k, t;for (i = 1; i <= n; ++ i) {for (j = 1; j <= tot[c]; ++ j)sta[c][j] <<= 2;for (j = 1; j <= m; ++ j) {clear(h), cnt = 0;c ^= 1, tot[c] = 0;for (k = 1; k <= tot[c ^ 1]; ++ k) {int s = sta[c ^ 1][k];lnt num = dp[c ^ 1][k];int p = (s >> bit[j - 1]) & 3;int q = (s >> bit[j]) & 3;if (!mp[i][j]) {if (!p && !q) insert(s, num);}else if (!p && !q) {if (!mp[i + 1][j] || !mp[i][j + 1]) continue;s += (1 << bit[j - 1]) + (1 << (bit[j] + 1));//左括号是0, 右括号是1, 四进制在二进制下两位表示一字节 insert(s, num);}else if (!p && q) {if (mp[i][j + 1]) insert(s, num);if (mp[i + 1][j]) {s += (1 << bit[j - 1]) * q - (1 << bit[j]) * q;insert(s, num);}}else if (p && !q) {if (mp[i + 1][j]) insert(s, num);if (mp[i][j + 1]) {s += (1 << bit[j]) * p - (1 << bit[j - 1]) * p;insert(s, num);}}else if (p + q == 2) {int b = 1;for (t = j + 1; t <= m; ++ t) {int v = (s >> bit[t]) & 3;if (v == 1) b ++; //根据括号序列的性质if (v == 2) b --;if (!b) {s -= (1 << bit[t]);break;}}s -= (1 << bit[j - 1]) + (1 << bit[j]);insert(s, num);}else if (p + q == 4) {int b = 1;for (t = j - 2; ~t; -- t) {int v = (s >> bit[t]) & 3;if (v == 2) b ++;if (v == 1) b --;if (!b) {s += (1 << bit[t]);break;}}s -= (1 << (bit[j - 1] + 1)) + (1 << (bit[j] + 1));insert(s, num);}else if (p == 2 && q == 1) {s -= (1 << (bit[j - 1] + 1)) + (1 << bit[j]);insert(s, num);}else if (p == 1 && q == 2) {if (i == ex && j == ey) ans += num;}}}}printf("%lld\n", ans);}int main() {scanf("%d%d", &n, &m);for (int i = 0; i < 26; ++ i)bit[i] = i << 1;for (int i = 1; i <= n; ++ i) {scanf("%s", s + 1);for (int j = 1; j <= m; ++ j)if (s[j] == '.') mp[i][j] = 1, ex = i, ey = j;}Plug_Dp();return 0;}


原创粉丝点击