Tony ' s Tour
Time Limit: 1000MS |
|
Memory Limit: 30000K |
Total Submissions: 3545 |
|
Accepted: 1653 |
Description A Square Township have been divided up into n*m (n rows and M columns) square plots (1<=n,m<=8), some of them is block Ed, others is unblocked. The Farm is located in the lower left plot and the "the" 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 would count how many unique tours Betsy can take in the going from the Farm to the market.
Input The input contains several test cases. The first line of all test case contain the 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 was followed by the zeros.
Output The For each test case output the answer to a single line.Sample Input 2 2....2 3# ..... 3 4............0 0
Sample Output 114
Source [email protected] |
Plug DP.
Add. ######## at the end of the grid. Turns the original problem into a loop problem, i.e. "URAL 1519"
..................
Why can't I just add one line ....... It?
This may result in an increase in the answer! Can snake away!
#include <iostream> #include <cstring> #include <algorithm> #include <cmath> #include < cstdio> #include <cstdlib> #define LL long long#define M 100000+5using 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& Lt;<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), and 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;}
"POJ 1739" Tony ' s tour