URAL 1519 Formula 1 插头DP

开始刷cdq ppt的题。。
不过毕竟插头DP写起来很长,所以为了写短一点我就没有在意运行时间了。。

学到了位运算清零。。

#include <cstdio>
#include <algorithm>
#include <map>
#define rep(i,j,k) for(int i=j;i<k;++i)
using namespace std;
const int N = 20001, M = 5001;
typedef long long ll;
typedef map<ll, ll>::iterator mll;
const int state[] = {0, -1, 1, 0};

int get_bit(int sta, int i) { return (sta >> (i << 1)) & 3; }
int bracket(int sta, int i, int rate) {
    int ret = i, cnt = rate;
    for (int cnt = rate; cnt; cnt += state[get_bit(sta, ret)]) ret -= rate;
    return ret;
}
int left(int sta, int i) { return bracket(sta, i, 1); }
int right(int sta, int i) { return bracket(sta, i, -1); }
int set_bit(int &sta, int i, int x) {
    sta = (sta & ~(3 << (i << 1))) | (x << (i << 1));
}

map<ll, ll> hash[2], *cur, *last;
int bx, by, n, m;
char mp[16][16];

void update(int x, int y, int sta, ll tv) {
    int l = y == 0 ? 0 : get_bit(sta, y), s;
    int t = x == 0 ? 0 : get_bit(sta, y + 1);
    #define create(i,j) s=sta,set_bit(s,y,i),set_bit(s,y+1,j),(*cur)[s]+=tv
    if (mp[x][y] == '*') {
        if (!l && !t) create(0, 0);
        return;
    }
    if (!l && !t) {
        if (x != n - 1 && y != m - 1) create(1, 2);
    } else if (!l || !t) {
        if (x < n - 1) create(l + t, 0);
        if (y < m - 1) create(0, l + t);
    } else {
        s = sta; set_bit(s, y, 0); set_bit(s, y + 1, 0);
        if (l == 1 && t == 1) set_bit(s, right(s, y + 1), 1);
        else if (l == 1 && t == 2) { if (x != bx || y != by) return; }
        else if (l == 2 && t == 1);
        else if (l == 2 && t == 2) set_bit(s, left(s, y + 1), 2);
        (*cur)[s]+=tv;
    }
}

ll solve() {
    int sz;
    cur = hash; last = hash + 1;
    last->clear(); (*last)[0] = 1;
    rep(i,0,n) {
        int rate = 2;
        rep(j,0,m) {
            cur->clear();
            for(mll k=last->begin();k!=last->end();++k)
                update(i, j, k->first << rate, k->second);
            rate = 0;
            swap(cur, last);
        }
    }
    ll ans = 0;
    for(mll k=last->begin();k!=last->end();++k)
        if (k->first == 0) {
            ans = k->second; break;
        }
    return ans;
}

int main() {
    while (scanf("%d%d", &n, &m) != EOF) {
        rep(i,0,n) scanf("%s", mp[i]);
        bx = by = -1;
        for(int i=n-1;i>=0;--i) for(int j=m-1;j>=0;--j)
            if (mp[i][j] == '.') bx = i, by = j, i = -1, j = -1;
        printf("%I64d\n", solve());
    }
    return 0;
}

URAL 1519 Formula 1

Description

Background

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.
Problem illustration

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

Sample 1

Input
4 4
**..
....
....
....
Output
2

Sample 2

Input
4 4
....
....
....
....
Output
6
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值