http://acm.hdu.edu.cn/showproblem.php?pid=3853
Membrane Monkey Shochu Madoka
Defined as Dp[i][j] position for the magic value of ij expected, can be found dp[i][j] = dp[i][j]*p1[i][j]+dp[i][j+1]*p2[i][j]+dp[i+1][j]*p3[i][j]+2
The last can be expressed as
1.0/(1-p[x][y][0]) * (p[x][y][1]*dp[x][y+1]+p[x][y][2]*dp[ x+1][y]+2.0);
Recursion starts at the lower-right corner and pushes the upper-left corner.
When a pit is p1=1, it will go wrong, so you have to give a special sentence. Skip directly.
#include <cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespacestd;intr,c;Doubledp[1010][1010],p[1010][1010][3];intMain () { while(~SCANF ("%d%d",&r,&C)) { for(intI=1; i<=r;i++) { for(intj=1; j<=c;j++) {scanf ("%LF%LF%LF", &p[i][j][0],&p[i][j][1],&p[i][j][2]); }} memset (DP,0,sizeofDP); for(intx=r;x>=1; x--) { for(inty=c;y>=1; y--) { if(X==R&&Y==C)Continue; if(ABS (p[x][y][0]-1.0) < (1e-7))Continue; Dp[x][y]=1.0/(1-p[x][y][0]) * (p[x][y][1]*dp[x][y+1]+p[x][y][2]*dp[x+1][y]+2.0); } } /*for (int i=1;i<=r;i++) {for (int j=1;j<=c;j++) {printf (" %lf ", Dp[i][j]); } printf ("\ n"); } */printf ("%.3lf\n", dp[1][1]); }}
Pre-bedtime small dp-hdu3853-probability dp