bzoj1814: Ural 1519 Formula 1 插头dp

bzoj1814: Ural 1519 Formula 1

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

分析

插头Dp入门题
插头Dp教程:cdq的教程
qtmd插头Dp
分类神烦。具体看注释就好了。
采用四进制+Hash,冲突暴力挪就好了。

代码

/**************************************************************
    Problem: 1814
    User: 2014lvzelong
    Language: C++
    Result: Accepted
    Time:364 ms
    Memory:4808 kb
****************************************************************/

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
//qtmd插头dp 
const int lim = 99991;
bool a[20][20], c;
char str[20];
int h[lim], tot[2], n, m, ex, ey;
long long dp[2][lim], st[2][lim], ans;
void Add(long long s, long long val) {
    int pos = s % lim;
    for(;~h[pos]; pos = (pos + 1 == lim ? 0 : pos + 1))
        if(st[c][h[pos]] == s) return void(dp[c][h[pos]] += val);
    h[pos] = ++tot[c]; st[c][tot[c]] = s; dp[c][tot[c]] = val;
}
void Clear() {
    for(int i = 1;i <= tot[c]; ++i) st[c][i] = dp[c][i] = 0;
    tot[c] = 0; memset(h, -1, sizeof(h));
}
void Dp() {
    for(int i = 1;i <= n; ++i) {
        for(int j = 1;j <= m; ++j) {
            c ^= 1; Clear(); 
            for(int k = 1;k <= tot[c ^ 1]; ++k) {
                long long s = st[c ^ 1][k], val = dp[c ^ 1][k];
                int x = ((j - 1) << 1), y = x + 2, p = (s >> x) & 3, q = (s >> y) & 3;
                long long t = s ^ (p << x) ^ (q << y);
                if(!a[i][j]) {if(!p && !q) Add(t, val); continue;}
                if(!p && !q) {//New
                    if(a[i + 1][j] && a[i][j + 1]) 
                        Add(s | (1 << x) | (2 << y), val);
                }
                else if(p && q) { //Merge
                    if(p == 2 && q == 1) {Add(t, val); continue;}
                    if(p == 1 && q == 2) {
                        if(i == ex && j == ey) ans += val;
                        continue;
                    }
                    if(p == 1 && q == 1) {
                        int u = y + 2;
                        for(int b = 0; ~b && u <= (m << 1); u += 2) 
                            b += ((t >> u) & 1) - ((t >> u + 1) & 1);
                        Add(t ^ (3 << u - 2), val);
                        continue;
                    }
                    if(p == 2 && q == 2) {
                        int u = x - 2;
                        for(int b = 0; ~b && u >= 0; u -= 2) 
                            b += ((t >> u + 1) & 1) - ((t >> u) & 1);
                        Add(t ^ (3 << u + 2), val);
                        continue;
                    }
                }
                else { //Remain
                    if(a[i + 1][j]) Add(t | (p + q << x), val);
                    if(a[i][j + 1]) Add(t | (p + q << y), val);
                }
            }
        } 
        for(int j = 1; j <= tot[c]; ++j) st[c][j] <<= 2;
    }
}
int main() {
    scanf("%d%d", &n, &m);
    c = 0; tot[c] = 1; st[c][1] = 0; dp[c][1] = 1; 
    for(int i = 1;i <= n; ++i) {
        scanf("%s", str + 1);
        for(int j = 1;j <= m; ++j) {
            a[i][j] = str[j] == '.';
            if(a[i][j]) ex = i, ey = j;
        }
    }
    Dp(); printf("%lld\n", ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/lvzelong2014/article/details/79830909