poj1739之插头DP
来源:互联网 发布:java时间字符串转整形 编辑:程序博客网 时间:2024/05/17 06:15
Tony's Tour
Time Limit: 1000MS Memory Limit: 30000KTotal 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.
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.
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
114
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <string>#include <queue>#include <algorithm>#include <map>#include <cmath>#include <iomanip>#define INF 99999999typedef 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;}
0 1
- poj1739之插头DP
- hdu1693之插头DP
- ural1519之插头DP
- fzu1977之插头DP
- poj3133之经典插头DP
- 【连通性状态压缩DP】poj1739
- POJ1739
- 插头DP
- 插头dp
- 插头dp
- 插头dp
- 插头DP
- 插头dp
- hdu1964之插头DP求最优值
- 动态规划之插头DP入门
- POJ1739 基于连通性的状态压缩dp
- poj 1739(男人八题之插头DP)
- hdu 1693 插头dp
- leetcode之Longest Consecutive Sequence
- 项目实践--广告排期系统
- Unity3D教程使用小技巧大合集
- 二叉树算法
- 随缘测试标题测试test1V656r49
- poj1739之插头DP
- 将word中数据分解到多个文本文件中
- ios 横竖屏控制
- Linux Shell syntax error: unexpected end of file
- 中国特色程序员的[钱途]
- 115个Java面试题和答案——终极列表(上)
- C++虚继承的古怪行为
- How to fix GC overhead limit exceeded in Eclipse
- android 获取本机ipv4地址