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
Lou one of the main eight, the same can be added to the line, but also special treatment, I write is added to the line.
#include <queue>#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intmn=177147;inti;structna{intx,z; Na (intXxintZZ): X (XX), Z (ZZ) {}};intn,m,x,y,z,a[ +],k,en,p1,p2;Charu[ -];BOOLmap[ +][ +];Long Longf[2][mn+1],ans;intv[2][mn+1];intre[ +][ +];queue<na>Q;inlineintGxintXintQ1,intQ2) {k=0; for(Registerinti=m+1; i;i--) k=k*3+ (i==x?q1: (i==x+1? Q2:a[i]));returnK;} InlinevoidUpintXintZLong LongLJ) {x++; K=x%2; if(v[k][z]!=x) v[k][z]=x,f[k][z]=0, Q.push (Na (x,z)); F[K][Z]+=LJ;}intMain () {registerinti,j; while(SCANF ("%d%d", &n,&m)! =EOF) { if(n==0&&m==0) Break; Ans=0; memset (Map,0,sizeof(map)); memset (V,0,sizeof(v)); memset (Re,0,sizeof(re)); Memset (F,0,sizeof(f)); for(i=1; i<=n;i++) {scanf ("%s",&T); for(j=1; j<=m;j++) Map[j][n-i+3]=u[j-1]=='.'; } N+=2; En=n*m-1; map[1][2]=1; map[m][2]=1; for(i=1; i<=m;i++) map[i][1]=1; while(!map[en%m+1][en/m+1]) en--; f[0][0]=v[0][0]=1; Q.push (Na (0,0)); while(!Q.empty ()) {na no=Q.front (); Q.pop (); Long Longan=f[no.x%2][no.z]; if(no.x%m==0) no.z*=3; X=no.x%m+1; y=no.x/m+1; for(i=1; i<=m+1; i++) a[i]=0; for(i=1, j=no.z;j;i++,j/=3) a[i]=j%3; if(!map[x][y]) up (NO.X,GX (x,0,0), an);Else if(a[x]==1&&a[x+1]==2){ if(no.x==en) ans+=an ; }Else if(a[x]==2&&a[x+1]==1) Up (NO.X,GX (x,0,0), an);Else if(a[x]==0&&a[x+1]==0){ if(map[x][y+1]&&map[x+1][y]) Up (NO.X,GX (x,1,2), an); }Else if(a[x]==0){ if(map[x+1][y]) Up (NO.X,GX (x,0, a[x+1]), an); if(map[x][y+1]) Up (NO.X,GX (x,a[x+1],0), an); }Else if(a[x+1]==0){ if(map[x+1][y]) Up (NO.X,GX (x,0, A[x]), an); if(map[x][y+1] Up (NO.X,GX (x,a[x),0), an); }Else if(a[x]==a[x+1]) {P1=p2=0; if(a[x]==1) for(j=0, i=x+2; i<=m;i++){ if(a[i]==1) j--; if(a[i]==2) J + +; if(j>0&&!P1) p1=i,j--; if(j>0&&P1) {p2=i; Break;} }Else for(j=0, i=x-1; i;i--){ if(a[i]==1) J + +; if(a[i]==2) j--; if(j>0&&!P2) p2=i,j--; if(j>0&&P2) {p1=i; Break;} } A[P1]=1; a[p2]=2; Up (NO.X,GX (x,0,0), an); }} printf ("%i64d\n", ans); }}
POJ 1739:tony ' s tour