ural 1519. Formula 1

插头dp第二题,debug了一个晚上,导致今天中秋都没有看到月亮,不过总算搞定了,思维习惯上打错个东西,找的好久,思路其实和前面轮廓线扫描差不多,但是要保证在最后一个合并最后两个插头,这样才能保证只有一条回路,单纯的二进制扫描法不能完成这个任务,所以要用括号表示法,cqd的ppt说的很明白,我就不说明啦,要用三进制表示,我们用二进制的两位来表示三进制,这样状态有2^(26),太多啦,就算空间允许,时间也过不去,所以要hash,这里我是参考小hh的一题模板的hash方法,这写法可以推广到很多地方,不过以前我喜欢用queue减状态但是后来因为空间不够改成了set,现在又学了这种hash好方法。

ID Date Author Problem Language Judgement result Test # Execution time Memory used
4481783 21:55:33
30 Sep 2012
xym 1519. Formula 1 C++ Accepted   0.125 1 236 KB
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<map>
#include<queue>
#define LL long long
using namespace std;
const int maxn=30001;
int n,m;
int mov[13]={0,2,4,6,8,10,12,14,16,18,20,22,24};
char gp[20][20],fx,fy;
inline LL getbit(LL st,int k){	return (st>>mov[k])&3; }
inline LL pybit(LL st,int k){return st<<mov[k];}
inline LL clrbit(LL st,int k){ return st&(~(3<<mov[k]));}
inline LL clrbit(LL st,int i,int j){ return st&(~(3<<mov[i]))&(~(3<<mov[j]));}
inline LL clrbit(LL st,int i,int j,int k){ return st&(~(3<<mov[i]))&(~(3<<mov[j]))&(~(3<<mov[k]));}
struct node
{
	int head[maxn],next[maxn],size;
	LL sum[maxn],sta[maxn];
	void clear()
	{
		memset(head,-1,sizeof(head));
		memset(sum,0,sizeof(sum));
		size=0;
	}
	void push(LL st,const LL v)
	{
		LL hash=st%maxn;
		for(int i=head[hash];i>=0;i=next[i])
		{
			if(sta[i]==st)
			{
				sum[i]+=v;
				return;
			}
		}
		sta[size]=st,sum[size]=v;
		next[size]=head[hash],head[hash]=size++;
	}
}dp[2];
int fl(int st,int pos)
{
	int cnt=1;
	for(int i=pos+1;i<=m;i++)
	{
		int k=((st>>mov[i])&3);
		if(k==1)
			cnt++;
		else if(k==2)
			cnt--;
		if(!cnt)
			return i;
	}
}
int fr(int st,int pos)
{
	int cnt=1;
	for(int i=pos-1;i>=0;i--)
	{
		int k=((st>>mov[i])&3);
		if(k==2)
			cnt++;
		else if(k==1)
			cnt--;
		if(!cnt)
			return i;
	}
}
LL DP()
{
	LL ans=0;
	dp[0].clear();
	dp[0].push(0,1);
	int now=0,pre=1;
	for(int i=1;i<=n;i++)
	{
		pre=now,now^=1;dp[now].clear();
		for(int j=0;j<dp[pre].size;j++)
			dp[now].push(dp[pre].sta[j]<<2,dp[pre].sum[j]);
		for(int j=1;j<=m;j++)
		{
			pre=now,now^=1;dp[now].clear();
			for(int k=0;k<dp[pre].size;k++)
			{
				LL l=getbit(dp[pre].sta[k],j-1);
				LL up=getbit(dp[pre].sta[k],j);
				LL st=clrbit(dp[pre].sta[k],j-1,j);
			//	cout<<i<<' '<<j<<' '<<l<<' '<<up<<' '<<st<<' '<<dp[pre].sta[k]<<' '<<dp[pre].sum[k]<<endl;
				if(!l&&!up)
				{
					if(gp[i][j]=='*')
					{
						dp[now].push(st,dp[pre].sum[k]);
						continue;
					}
					if(i<n&&j<m&&gp[i+1][j]=='.'&&gp[i][j+1]=='.')
					dp[now].push(st|pybit(1,j-1)|pybit(2,j),dp[pre].sum[k]);
				}
				else if(!l||!up)
				{
					int e=l==0?up:l;
					if(i<n&&gp[i+1][j]=='.')
					dp[now].push(st|pybit(e,j-1),dp[pre].sum[k]);
					if(j<m&&gp[i][j+1]=='.')
					dp[now].push(st|pybit(e,j),dp[pre].sum[k]);
				}
				else if(l==1&&up==1)
					dp[now].push(st^pybit(3,fl(st,j)),dp[pre].sum[k]);
				else if(l==2&&up==2)
					dp[now].push(st^pybit(3,fr(st,j-1)),dp[pre].sum[k]);
				else if(l==2&&up==1)
					dp[now].push(st,dp[pre].sum[k]);
				else if(i==fx&&j==fy)
					dp[now].push(st,dp[pre].sum[k]);
			}
		}
	}
	for(int i=0;i<dp[now].size;i++)
		if(dp[now].sta[i]==0)
			return dp[now].sum[i];
	return 0;
}
int main()
{
	//freopen("text1.out","w",stdout);
	while(~scanf("%d%d",&n,&m))
	{
		for(int i=1;i<=n;i++)
			scanf("%s",&gp[i][1]);
		fx=0;
		for(int i=n;i>0&&!fx;i--)
		{
			for(int j=m;j>0&&!fx;j--)
			{
				if(gp[i][j]=='.')
					fx=i,fy=j;
			}
		}
		cout<<DP()<<endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值