poj1739之插头DP

Tony's Tour
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 3256 Accepted: 1510

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


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <iomanip>
#define INF 99999999
typedef long long LL;
using namespace std;

const int MAX=1000+10;
const int N=8+10;
int n,m,size,index;
int mp[N][N],total[2],bit[N];
int head[MAX],next[MAX],Hash[MAX];
LL dp[2][MAX],state[2][MAX],sum;

void Init(){
	memset(mp,0,sizeof mp);
	sum=index=0;
	total[index]=1;
	dp[index][1]=1;
	state[index][1]=0;
}

void HashCalState(LL s,LL num){
	int pos=s%MAX;
	for(int i=head[pos];i != -1;i=next[i]){
		if(state[index][Hash[i]] == s){
			dp[index][Hash[i]]+=num;
			return;
		}
	}
	++total[index];
	state[index][total[index]]=s;
	dp[index][total[index]]=num;
	//头插法
	Hash[size]=total[index];
	next[size]=head[pos];
	head[pos]=size++; 
}

void DP(){
	for(int i=1;i<=n;++i){
		for(int k=1;k<=total[index];++k)state[index][k]<<=2;//由上一行到本行
		for(int j=1;j<=m;++j){
			memset(head,-1,sizeof head);
			size=0;
			index=index^1;
			total[index]=0;
			for(int k=1;k<=total[index^1];++k){
				LL s=state[index^1][k];
				LL num=dp[index^1][k];
				int p=(s>>bit[j-1])%4;
				int q=(s>>bit[j])%4;
				if(!mp[i][j]){
					if(!p && !q)HashCalState(s,num);
				}else if(!p && !q){
					if(!mp[i][j+1] || !mp[i+1][j])continue;
					s=s+(1<<bit[j-1])+2*(1<<bit[j]);
					HashCalState(s,num);
				}else if(!p && q){
					if(mp[i][j+1])HashCalState(s,num);
					if(mp[i+1][j]){
						s=s+q*(1<<bit[j-1])-q*(1<<bit[j]);
						HashCalState(s,num);
					}
				}else if(p && !q){
					if(mp[i+1][j])HashCalState(s,num);
					if(mp[i][j+1]){
						s=s-p*(1<<bit[j-1])+p*(1<<bit[j]);
						HashCalState(s,num);
					}
				}else if(p == 1 && q == 1){
					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])-2*(1<<bit[t]);
							break;
						}
					}
					s=s-(1<<bit[j-1])-(1<<bit[j]);
					HashCalState(s,num);
				}else if(p == 2 && q == 2){
					int b=1;
					for(int t=j-2;t>=0;--t){
						int v=(s>>bit[t])%4;
						if(v == 2)++b;
						if(v == 1)--b;
						if(!b){
							s=s-(1<<bit[t])+2*(1<<bit[t]);
							break;
						}
					}
					s=s-2*(1<<bit[j-1])-2*(1<<bit[j]);
					HashCalState(s,num);
				}else if(p == 2 && q == 1){
					s=s-2*(1<<bit[j-1])-(1<<bit[j]);
					HashCalState(s,num);
				}//else if(p == 1 && q == 1)//不能有回路 
			}
		} 
	}
	for(int i=1;i<=total[index];++i)sum+=dp[index][i];
}

int main(){
	char ch;
	for(int i=0;i<N;++i)bit[i]=i<<1;
	while(~scanf("%d%d",&n,&m),n+m){
		Init();
		for(int i=1;i<=n;++i){
			getchar();
			for(int j=1;j<=m;++j){
				scanf("%c",&ch);
				mp[i][j]=(ch == '.');
			}
		}
		mp[n+1][1]=mp[n+1][m]=1;
		DP();
		printf("%lld\n",sum);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值