ural1519Formula 1 (插头DP)

题意:n*m的地图,'*'代表不能走,'.'代表可以走。求哈密顿回路的数量。n,m<=12.

claris讲了插头DP,感觉都差不多听懂了,但是实现还是有一定困难,花了3个小时才写出来我的第一篇插头DP的程序。感觉就是维护一个轮廓线,每一格压位表示,然后DP过程中每一次对这个轮廓线的维护都是O(1)的。

这是用括号表示的,0表示空,1表示左括号,2表示右括号。注意分类讨论就行了。实现的时候还是要hash一下。还有注意位运算不要写错了,因为位运算一旦错了很难发现。

#include<cstdio>
#include<cstring>
#include<cassert>
#include<iostream>
using namespace std;
#define LL long long
#define clr(a) memset(a,0,sizeof a)
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define erp(i,a,b) for(int i=a;i>=b;--i)
#define getbit(x,y) (((x) >> ((y)<<1)) & 3)
#define bit(x,y) ((x)<<((y)<<1)) 
#define clrbit(x,i,j) ((x) & (~(3<<((i)<<1))) & (~(3<<((j)<<1))))
const int MAXS = 1000000, mo = 100003;
int n, m, ex, ey;

char a[15][15];
struct Node
{
	int s, nxt;
	LL val;
};
struct Hash
{
	Node e[MAXS];
	int adj[mo], ec;
	void init()
	{
		memset(adj, -1, sizeof adj);
		ec = 0;
	}
	void push(int s, LL v)
	{
		int ha = s%mo;
		for (int i = adj[ha]; ~i; i=e[i].nxt)
			if (e[i].s == s)
			{
				e[i].val += v;
				return;
			}
		e[ec].val = v, e[ec].s = s;
		e[ec].nxt = adj[ha];
		adj[ha] = ec++;
	}
} dp[2];

inline int FindL(int st, int x)
{
	int cnt = 1, s;
	erp(i, x-1, 0)
	{
		s = (st >> (i<<1)) & 3;
		if (s == 2) cnt++;
		else if (s == 1) cnt--;
		if (!cnt) return i;
	}
	return -1;
}
inline int FindR(int st, int x)
{
	int cnt = 1, s;
	rep(i, x+1, m)
	{
		s = (st >> (i<<1)) & 3;
		if (s == 1) cnt++;
		else if (s == 2) cnt--;
		if (!cnt) return i;
	}
	return -1;
}

void work1(int i, int j, int cur) // '.'
{
	dp[cur].init();
	rep(k, 0, dp[cur^1].ec-1)
	{
		int L = getbit(dp[cur^1].e[k].s, j-1);
		int U = getbit(dp[cur^1].e[k].s, j);
		int s = clrbit(dp[cur^1].e[k].s, j-1, j);
		LL las = dp[cur^1].e[k].val;
		if (!L && !U)
		{
			if (i<n && j<m)
				dp[cur].push(s|bit(1, j-1)|bit(2, j), las);
		}
		else if (!L)
		{
			if (i < n) dp[cur].push(s|bit(U, j-1), las);
			if (j < m) dp[cur].push(s|bit(U, j), las);
		}
		else if (!U)
		{
			if (i < n) dp[cur].push(s|bit(L, j-1), las);
			if (j < m) dp[cur].push(s|bit(L, j), las);
		}
		else if (L==1 && U==1)
			dp[cur].push(s^bit(3, FindR(s, j)), las);
		else if (L==2 && U==2)
			dp[cur].push(s^bit(3, FindL(s, j-1)), las);
		else if (L==2 && U==1)
			dp[cur].push(s, las);
		else if (i==ex && j==ey) // L==1&&R==2
			dp[cur].push(s, las);
	}
}

void work2(int i, int j, int cur)
{
	dp[cur].init();
	rep(k, 0, dp[cur^1].ec-1)
	{
		int L = getbit(dp[cur^1].e[k].s, j-1);
		int U = getbit(dp[cur^1].e[k].s, j);
		int s = clrbit(dp[cur^1].e[k].s, j-1, j);
		LL las = dp[cur^1].e[k].val;
		if (!L && !U) dp[cur].push(s, las);
	}
}

LL solve()
{
	dp[0].init(), dp[0].push(0, 1);
	int cur = 0;
	rep(i, 1, n)
	{
		rep(k, 0, dp[cur].ec-1)
			dp[cur].e[k].s <<= 2;
		rep(j, 1, m)
		{
			cur ^= 1;
			if (a[i][j]=='.') work1(i, j, cur);
			else if (a[i][j]=='*') work2(i, j, cur);
		}
	}
	rep(k, 0, dp[cur].ec-1)
		if (!dp[cur].e[k].s) return dp[cur].e[k].val;
	return 0;
}

int main()
{
	scanf("%d%d", &n, &m);
	rep(i, 1, n)
	{
		scanf("%s", a[i]+1);
		rep(j, 1, m)
			if (a[i][j]=='.')
				ex = i, ey = j;
	}
	cout << solve() << '\n';
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值