Tony's Tour
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
LouTiancheng@POJ
|
插头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; }