Backgroundregardless of the fact, that Vologda could don't get rights to hold the Winter Olympic games of 20**, it's well-k Nown, that's the city would conduct one of the Formula 1 events. Surely, for such an important thing a new race circuit should being built as well as hotels, restaurants, international Airpo Rt-everything for Formula 1 fans, who'll flood the city soon. But if all the hotels and a half of the restaurants were built, it appeared, which at the site for the future circuit a l OT of Gophers lived in their holes. Since We like animals very much, ecologists would never allow to build the race circuit over the holes. The mayor is sitting sadly in his office and looking at the map of the circuit with all the holes plotted on it. Problemwho would be smart enough to draw a plan of the the circuit and keep the city from inevitable disgrace? Of course, only true professionals-battle-hardened programmers from the first team of local Technical University!.. But we heroes were notLooking for easy life and set much more difficult problem: "Certainly, our mayor would be glad, if we find how many ways of Building the circuit is there! "-they said. It should be said, which the circuit in Vologda are going to being rather simple. It'll be a rectangle
N*
MCells in size with a single circuit segment built through each cell. Each segment should is parallel to one of rectangle's sides, so only right-angled bends is on the circuit. At the picture below and samples is given for
N=
M= 4 (gray squares mean gopher holes, and the bold black line means the race circuit). There is no other ways to build the circuit here. Inputthe first line contains the integer numbers
Nand
M(2≤
N,
M≤12). Each of the next
NLines contains
MCharacters, which is the corresponding cells of the rectangle. Character "." (full stop) means a cell, where a segment of the race circuit should is built, and character "*" (asterisk)-a cell, wher E A gopher hole is located. Outputyou should output the desired number of ways. It is guaranteed, the it does not exceed 263-1.samples
input |
Output |
4 4** ........ |
2 |
4 4 .......... |
6 |
Plug DP
#include <queue>#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intBi=1e8;inti;structna{intx,z; Na (intXxintZZ): X (XX), Z (ZZ) {}};structbig{inta[4]; Big () {memset (a),0,sizeof(a)); a[0]=1; } Bigoperator=(intx) { if(x==0) {memset (A,0,sizeof(a)); a[0]=1; return* This; } a[0]=0; while(x) {a[0]++; a[a[0]]=x%i; X/=i; } if(!a[0]) a[0]=1; return* This; } Bigoperator+(ConstBig &x) {Big R; if(a[0]<x.a[0]) r.a[0]=x.a[0];Elser.a[0]=a[0]; for(i=1; i<=r.a[0];i++) r.a[i]=a[i]+X.a[i]; for(i=1; i<=r.a[0];i++) if(r.a[i]>=i) {R.a[i]-=bi;r.a[i+1]++; if(i==r.a[0]) r.a[0]++; } returnR; }}ans;intn,m,x,y,z,a[ +],k,p1,p2,end;BOOLmap[ +][ +];big f[2][1594324];intv[2][1594324];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;} InlinevoidUpintXintZ,big LJ) {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]=f[k][z]+LJ;}Charc[Ten];intMain () {registerinti,j; scanf ("%d%d",&n,&m); for(j=1; j<=n;j++) {scanf ("%s", c); for(i=1; i<=m;i++) Map[i][j]=c[i-1]=='.'; } End=n*m-1; while(!map[end%m+1][end/m+1]) end--; f[0][0]=v[0][0]=1; Q.push (Na (0,0)); while(!Q.empty ()) {na no=Q.front (); Q.pop (); Big An=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==end) ans=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 ("%d", ans.a[ans.a[0]]); for(i=ans.a[0]-1; i;i--) printf ("%08d", Ans.a[i]);}
Ural 1519 Formula 1