Layer I has the possible position of the 2*i+1 species (from the 1th vacancy to the last vacancy in a total of i+1 and I nail positions), with D[I][J] to indicate the probability of the first row in the J-position of the molecule (the denominator is 2^i).
If the location is empty, then there are 3 cases:
(1). Drop down from the previous position,
(2). Drop to the left of the nail (if any) and go to the right,
(3). Drop to the right of the nail (if available) and go left.
If there is a nail in the position, there are 2 kinds of cases:
(1). There are nails in this position, so it is not possible to drop in this position,
(2). This position has no nails and can be dropped from the previous position.
#include < stdio.h> #include <stdlib.h> #include <string.h>typedef long long ll;int a[110][110]; LL D[110][210];char e[1100];int Main (void) {int i,j,n,m,top,lo; LL p,q,sump;scanf ("%d%d", &n,&m); while (getchar () = = ") {;} for (i=1;i<=n;i++) {gets (e); Lo=strlen (e); Top=0;for (j=0;j<lo;j++) {if (e[j]== ' * ') {top++;a[i][top]=1;} else if (e[j]== '. ') {top++;a[i][top]=0;}}} if (a[1][1]==1) {d[1][1]=d[1][3]=1;d[1][2]=0;} else{d[1][1]=d[1][3]=0;d[1][2]=2;} for (i=2;i<=n;i++) {d[i][1]= (a[i][1]==1)? d[i-1][1]:0;d[i][2*i+1]= (a[i][i]==1)? D[i-1][2*i-1]:0;for (j=2;j<2* i+1;j++) {if (j%2==1) {sump=d[i-1][j-1]*2;sump=sump+ (a[i][j/2]==1) d[i-1][j-2]:0); sump=sump+ ((a[i][j/2+1]==1)? d[ i-1][j]:0);} else{sump= (a[i][j/2]==1) 0:2*d[i-1][j-1];} D[i][j]=sump;}} if (q==0) {printf ("0/1\n");} Else{p=d[n][2*m+1];q= (LL) 1<<n;while ((p%2==0) && (q%2==0)) {P=P/2;Q=Q/2;} printf ("%lld/%lld\n", P,q);} return 0;}
POJ 1189-Nails and pellets (DP)