Formula 1 (插头)

1519. Formula 1

Time limit: 1.0 second
Memory limit: 64 MB

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. There are at least 4 cells without gopher holes.

Output

You should output the desired number of ways. It is guaranteed, that it does not exceed 2 63-1.

Samples

inputoutput
4 4
**..
....
....
....
2
4 4
....
....
....
....
6















参考:《基于连通性状态压缩的动态规划问题 》——  长沙市雅礼中学 陈丹琦

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
int mp[15][15];
int Hex[15];//  用2进制 ->  四进制
int Hash[200*10000+100];
int k,n,m,N,M;//k:滚动数组
LL ans;
int total[2];
LL state[2][100*10000+10];//状态
LL sum[2][100*10000+10];//对应状态 的数量
const int mod=200*10000+1;
void init()
{
	ans=0;
	k=0;
	memset(mp,0,sizeof(mp));
	for(int i=0;i<=14;i++)
		Hex[i]=i*2;
	total[0]=1;
	sum[0][1]=1;
}
void _init(int x)
{
	total[x]=0;
	memset(Hash,0,sizeof(Hash));
	memset(sum[x],0,sizeof(sum[x]));
	memset(state[x],0,sizeof(state[x]));
}
void To_Hash(LL s,LL t)//哈稀
{
	int x=s%mod;

	while(Hash[x])
	{
		if(state[k][Hash[x]]==s)
		{
			sum[k][Hash[x]]+=t;
			return;
		}

		x++;
		x%=mod;
	}

	total[k]++;
	Hash[x]=total[k];
	state[k][total[k]]=s;
	sum[k][total[k]]=t;
	
}
void dp_ct()
{


//轮廓线上的插头都是成对出现的且都不可能相交
//括号匹配  ( :轮廓线上成对的第一个插头 
//          ):轮廓线上成对的第二个插头 
//共三种状态 -> 用四进制 ->   1:( 
//                          2:)
//                          0:无插头



        for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			int k1=k;//上一格
			k^=1;//当前格
			_init(k);

			for(int l=1;l<=total[k1];l++)//枚举上一格的全部状态
			{
				LL s=state[k1][l];//取状态
				LL num=sum[k1][l];//取 s状态 的数量

				int x=(s>>Hex[j-1])%4;//轮廓线上 左
				int y=(s>>Hex[j])%4;//轮廓线上  上

				if(mp[i][j])//无障碍
				{
					if(!x&&!y)//无左插头 无上插头
					{
						if(mp[i][j+1]&&mp[i+1][j])
						{

							s+=(1*(1<<Hex[j-1])+2*(1<<Hex[j]));
							To_Hash(s,num);

						}
					}
					else if(!x&&y)// 无左插头 有上插头
					{
						if(mp[i][j+1]) //走右
							To_Hash(s,num);
						if(mp[i+1][j])// 走下
						{
							s-=y*(1<<Hex[j]);
							s+=y*(1<<Hex[j-1]);
							To_Hash(s,num);
						}
					}
					else if(x&&!y)//有左插头  无上插头
					{
						if(mp[i+1][j])//走下
							To_Hash(s,num);
						if(mp[i][j+1])//走右
						{
							s-=x*(1<<Hex[j-1]);
							s+=x*(1<<Hex[j]);
							To_Hash(s,num);
						}
					}
					else if(x==1&&y==1)//都有插头 且是((   合并
					{
						int flag=1;
						for(int ii=j+1;ii<=n;ii++)
						{
							int Ls=(s>>Hex[ii])%4;
							if(Ls==2)
								flag--;
							if(Ls==1)
								flag++;
							if(!flag)
							{
								s-=(1<<Hex[ii]);//s=s-2*(1<<Hex[ii])+1*(1<<Hex[ii]);
								break;
							}
						}
						
						s-=1*(1<<Hex[j-1]);
						s-=1*(1<<Hex[j]);
						To_Hash(s,num);	
					}
					else if(x==2&&y==2)//都有插头 且是))  合并
					{
						int flag=1;
						for(int ii=j-2;ii>=1;ii--)
						{
							int Ls=(s>>Hex[ii])%4;
							if(Ls==1)
								flag--;
							if(Ls==2)
								flag++;
							if(!flag)
							{
								s+=(1<<Hex[ii]);//s=s-1*(1<<Hex[ii])+2*(1<<Hex[ii]);
								break;
							}
						}
						s-=2*(1<<Hex[j-1]);
						s-=2*(1<<Hex[j]);
						To_Hash(s,num);
					}
					else if(x==1&&y==2)//都有插头  且()   合并
					{
						if(i==N&&j==M)
							ans=num;
					}
					else if(x==2&&y==1)//都有插头 且)(    合并
					{	
						s-=2*(1<<Hex[j-1]);
						s-=1*(1<<Hex[j]);
						To_Hash(s,num);
					}
				}
				else //有障碍
				{
					if(!x&&!y)
					{
						To_Hash(s,num);
					}
				}
			}
		}

		for(int ii=1;ii<=total[k];ii++)
			state[k][ii]=(state[k][ii]<<2);//四进制 换行时 行首左为0  
	}

}
int main()
{
	while(scanf("%d %d",&n,&m)!=EOF)
	{
		init();
		getchar();
		char ch;
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				scanf("%c",&ch);
				mp[i][j]=(ch=='.');
				if(mp[i][j])
					N=i,M=j;
			}
			getchar();
		}
		dp_ct();
		printf("%lld\n",ans);
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值