【POJ 1739】Tony's Tour

Tony's Tour
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 3545 Accepted: 1653

Description

A square township has been divided up into n*m(n rows and m columns) square plots (1<=N,M<=8),some of them are blocked, others are unblocked. The Farm is located in the lower left plot and the Market is located in the lower right plot. Tony takes her tour of the township going from Farm to Market by walking through every unblocked plot exactly once. 
Write a program that will count how many unique tours Betsy can take in going from Farm to Market. 

Input

The input contains several test cases. The first line of each test case contain two integer numbers n,m, denoting the number of rows and columns of the farm. The following n lines each contains m characters, describe the farm. A '#' means a blocked square, a '.' means a unblocked square. 
The last test case is followed by two zeros. 

Output

For each test case output the answer on a single line.

Sample Input

2 2
..
..
2 3
#..
...
3 4
....
....
....
0 0

Sample Output

1
1
4

Source



插头dp。


在方格最后添加.########.  使得原问题转化为回路问题,即【URAL 1519】

                         ..................


为什么不能只添加一行...............呢?


这样可能会使得答案增加!可以蛇形走!


#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#define LL long long
#define M 100000+5
using namespace std;
char s[20];
int total[2],tot,now,n,m,pre,bit[20],a[20][20],h[1005];
struct edge
{
	int y,ne;
}e[M];
LL f[2][M],state[2][M],ans;
void Solve(LL s,LL num)
{
	int pos=s%1000;
	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()
{
	ans=0;
	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++)
		{
			pre=now,now=pre^1;
			memset(h,0,sizeof(h));
			total[now]=0;
			tot=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])
						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-=(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+=(1<<bit[t]);
							break;
						}
					}
					s=s-2*(1<<bit[j])-2*(1<<bit[j-1]);
					Solve(s,num);
				}
				else if (p==1&&q==2)
				{
					if (i==n&&j==m)
						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;
	while (scanf("%d%d",&n,&m)!=EOF&&n)
	{
		memset(a,0,sizeof(a));
		for (int i=1;i<=n;i++)
		{
			scanf("%s",s+1);
			for (int j=1;j<=m;j++)
				a[i][j]=s[j]=='.';
		}
		n+=2;
		a[n-1][1]=a[n-1][m]=1;
		for (int j=2;j<m;j++)
			a[n-1][j]=0;
		for (int j=1;j<=m;j++)
			a[n][j]=1;
		Plugdp();
		cout<<ans<<endl;
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值