您的位置:首页 > 其它

poj 2411| 插头dp

2015-11-16 10:39 190 查看
经典题: 用1*2的木板铺满n*m的矩形的方案数。做一下插头dp的模板。0表示这个位置不可以放木板(也就是已经放上了木板),1表示这个位置可以放木板(也就是没放木板)。转移有两种1.当前位置上方的格子如果没放,那么在这个格子放竖着的,如果放了,什么也不放2.当前位置上方放了,左面没放,那么可以放一个横着的。//别看代码长,大部分都是哈希表(虽然这道题用不到,以后呢?)
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>

#define md
#define ll long long
#define inf (int) 1e9
#define eps 1e-8
#define N 5010
using namespace std;
struct ha
{
	int sz;
	int a
,q
;
	ll f
;
	void clear()
	{
		memset(a,-1,sizeof(a));
		sz=0;
	}
	void insert(int x,ll d)
	{
		int pos=x%N;
		while (a[pos]!=-1&&a[pos]!=x) { pos++; if (pos>=N) pos-=N; }
		if (a[pos]==-1)
		{
			q[++sz]=pos; a[pos]=x; f[pos]=0;
		}
		f[pos]+=d;
	}
	ll find(int x)
	{
		int pos=x%N;
		while (a[pos]!=-1&&a[pos]!=x) { pos++; if (pos>=N) pos=N; }
		if (a[pos]==x) return f[pos];
			else return 0;
	}
} hash[2];

int get(int s,int p,int l=1)
{
	if (p<=0) return 0;
	return (s>>((p-1)*l))&((1<<l)-1);
}

void set(int &s,int p,int d,int l=1)
{
	s^=get(s,p)<<((p-1)*l);
	s|=d<<((p-1)*l);
}

int main()
{
	int n,m,now,pre;
	while (1)
	{
		scanf("%d%d",&n,&m);
		if (n==0&&m==0) break;
		if ((n&1)&&(m&1)) { printf("0\n"); continue;}
		now=1,pre=0;
		hash[now].clear(); hash[pre].clear();
		hash[now].insert(0,1);
		for (int i=1;i<=n;i++)
		{
			for (int j=1;j<=m;j++)
			{
				swap(now,pre);
				hash[now].clear();
				for (int k=1;k<=hash[pre].sz;k++)
				{
					int pos=hash[pre].q[k];
					int s=hash[pre].a[pos]; ll d=hash[pre].f[pos];
					int p=get(s,j),q=get(s,j-1);
					int S=s; set(S,j,p^1); hash[now].insert(S,d);
					if (p==0&&q==1)
					{
						S=s; set(S,j-1,0); hash[now].insert(S,d);
					}
				}
			}
		}
		printf("%lld\n",hash[now].find(0));		
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: