POJ 1739 Tony's Tour 插头DP

10 篇文章 0 订阅

相对于URAL 1519,本题是限定起终点的哈密尔顿路的个数。发现下面加2行就转化成哈密尔顿回路了。。
于是。。

#include <cstdio>
#include <algorithm>
#include <map>
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 set_bit(int &sta, int i, int x) {
    sta = (sta & ~(3 << (i << 1))) | (x << (i << 1));
}
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); }

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 v) {
    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]+=v
    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]+=v;
    }
}

ll solve() {
    int sz, i, j, rate;
    cur = hash; last = hash + 1;
    last->clear(); (*last)[0] = 1;
    for(i=0;i<n;++i) {
        rate = 2; // 轮廓线换行 
        for(j=0;j<m;++j) {
            cur->clear();
            for(mll k=last->begin();k!=last->end();++k) {
                if (rate == 2 && get_bit(k->first, m)) continue;
                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 && n && m) {
        for(int i=0;i<n;++i) scanf("%s", mp[i]);
        for(int i=0;i<m;++i) mp[n][i]='#'; mp[n][0] = mp[n][m-1] = '.'; ++n;
        for(int i=0;i<m;++i) mp[n][i]='.'; ++n;
        bx = n - 1, by = m - 1;
        printf("%I64d\n", solve());
    }
    return 0;
}

Tony’s Tour

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 3840 Accepted: 1785

Description

A square township has been divided up into n*m(n rows and m columns) square plots (1<=N,M<=8),some of them are blocked, others are unblocked. The Farm is located in the lower left plot and the Market is located in the lower right plot. Tony takes her tour of the township going from Farm to Market by walking through every unblocked plot exactly once.
Write a program that will count how many unique tours Betsy can take in going from Farm to Market.

Input

The input contains several test cases. The first line of each test case contain two integer numbers n,m, denoting the number of rows and columns of the farm. The following n lines each contains m characters, describe the farm. A ‘#’ means a blocked square, a ‘.’ means a unblocked square.
The last test case is followed by two zeros.

Output

For each test case output the answer on a single line.

Sample Input

2 2
..
..
2 3
#..
...
3 4
....
....
....
0 0

Sample Output

1
1
4
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值