码迷,mamicode.com
首页 > 其他好文 > 详细

【URAL 1519】【插头dp模板】Formula 1

时间:2015-04-03 09:31:08      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:ural   oi   插头dp   

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.
技术分享

Input

The first line contains the integer numbers N and M (2 ≤ NM ≤ 12). Each of the next N lines contains Mcharacters, 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.

Output

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

Samples

input output
4 4
**..
....
....
....
2
4 4
....
....
....
....
6


插头dp模板题。


非常好的文章


我代码的处理顺序是:

1.##-->()


2.#(-->#( 或 )#   #)-->#) 或 )#


3.(#-->(# 或 #(   )#-->)# 或 #)


4.((-->##


5.))-->##


6.()-->##


7.)(-->##


#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#define LL long long
#define M 300000+5
using namespace std;
char s[20];
int tot,a[15][15],n,m,total[2],now,pre,ex,ey,bit[20];
int h[M];
struct edge
{
	int y,ne;
}e[M];
LL f[2][M],state[2][M],ans=0;
void Solve(LL s,LL num)
{
	int pos=s%(M-5);
	for (int i=h[pos];i;i=e[i].ne)
		if (state[now][e[i].y]==s)
		{
			f[now][e[i].y]+=num;
			return;
		}
	total[now]++;
	state[now][total[now]]=s;
	f[now][total[now]]=num;
	e[++tot].y=total[now];
	e[tot].ne=h[pos];
	h[pos]=tot;
}
void Plugdp()
{
	now=0;
	total[now]=1;
	state[now][1]=0;
	f[now][1]=1;
	for (int i=1;i<=n;i++)
	{
		for (int j=1;j<=total[now];j++)
			state[now][j]<<=2;
		for (int j=1;j<=m;j++)
		{
			tot=0;
			memset(h,0,sizeof(h));
			pre=now,now=pre^1;
			total[now]=0;
			for (int k=1;k<=total[pre];k++)
			{
				LL s=state[pre][k];
				LL num=f[pre][k];
				int p=(s>>bit[j-1])%4,q=(s>>bit[j])%4;
				if (!a[i][j])
				{
					if (p+q==0) 
						Solve(s,num);
				}
				else if (p+q==0) 
				{
					if (!a[i][j+1]||!a[i+1][j]) 
						continue;
					s=s+(1<<bit[j-1])+2*(1<<bit[j]);
					Solve(s,num);
				}
				else if (!p&&q) 
				{
					if (a[i][j+1]) 
						Solve(s,num);
					if (a[i+1][j])
						s=s-q*(1<<bit[j])+q*(1<<bit[j-1]),
						Solve(s,num);
				}
				else if (p&&!q)
				{
					if (a[i+1][j]) 
						Solve(s,num);
					if (a[i][j+1])
						s=s-p*(1<<bit[j-1])+p*(1<<bit[j]),
						Solve(s,num);
				}
				else if (p+q==2)
				{
					int b=1;
					for (int t=j+1;t<=m;t++)
					{
						int v=(s>>bit[t])%4;
						if (v==1) b++;
						if (v==2) b--;
						if (!b)
						{
							s=s-(1<<bit[t]);
							break;
						}
					}	
					s=s-(1<<bit[j-1])-(1<<bit[j]);
					Solve(s,num);
				}
				else if (p+q==4)
				{
					int b=-1;
					for (int t=j-2;t>=0;t--)
					{
						int v=(s>>bit[t])%4;
						if (v==1) b++;
						if (v==2) b--;
						if (!b)
						{
							s=s+(1<<bit[t]);
							break;
						}
					}
					s=s-2*(1<<bit[j-1])-2*(1<<bit[j]);
					Solve(s,num);
				}
				else if (p==1&&q==2)
				{
					if (i==ex&&j==ey)
						ans+=num;
				}
				else if (p==2&&q==1)
				{
					s=s-2*(1<<bit[j-1])-(1<<bit[j]);
					Solve(s,num);
				}
			}
		}
	}
}
int main()
{
	for (int i=0;i<15;i++)
		bit[i]=i<<1;
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++)
	{
		scanf("%s",s+1);
		for (int j=1;j<=m;j++)
		{
			a[i][j]=s[j]=='.';
			if (s[j]=='.') ex=i,ey=j;
		}
	}
	Plugdp();
	printf("%lld\n",ans);
	return 0;
}

技术分享

【URAL 1519】【插头dp模板】Formula 1

标签:ural   oi   插头dp   

原文地址:http://blog.csdn.net/regina8023/article/details/44837541

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!