LOOPSTime

** limit:15000/5000 MS (java/others) Memory limit:125536/65536 K (java/others)**

Total submission (s): 1651 Accepted Submission (s): 653

Problem Descriptionakemi Homura is a Mahou shoujo (Puella magi/magical Girl).

Homura wants to help his friend Madoka save the world. But because of the plot of the Boss incubator, she's trapped in a labyrinth called LOOPS.

The planform of the LOOPS is a rectangle of r*c grids. There is a portal with each grid except the exit grid. It costs Homura 2 magic power to use a portal once. The portal in a grid g (r, c) would send Homura to the grid below G (Grid (r+1, C)), and the grid on the right of G (Grid (R, c+1) ), or even G itself at respective probability (what evil the Boss incubator is)!

At the beginning Homura are in the top left corner of the LOOPS ((1, 1)), and the exit of the labyrinth are in the bottom RI Ght Corner ((R, C)). Given the probability of transmissions of each portal, your task was help poor Homura calculate the EXPECT magic power She Need to escape from the LOOPS.

Inputthe first line contains, integers r and C (2 <= R, c <= 1000).

The following R lines, each contains c*3 real numbers, at 2 decimal places. Every three numbers make a group. The first, second and third number of the CTH group of line R represent the probability of transportation to Grid (R, c), Grid (R, c+1), Grid (R+1, c) of the portal in grid (R, c) respectively. Groups of numbers is separated by 4 spaces.

It is ensured the sum of three numbers in each group is 1, and the second numbers of the rightmost groups is 0 (as T Here is no grids on the right of them) while the third numbers of the Downmost groups is 0 (as there are no grids below them).

Ignore the last three numbers of the input data. They is printed just for looking neat.

The answer is ensured no greater than 1000000.

Terminal at EOF

Outputa real number at 3 decimal places (round to), representing the expect magic power Homura need to escape from the LOO Ps.

Sample Input

2 20.00 0.50 0.50 0.50 0.00 0.500.50 0.50 0.00 1.00 0.00 0.00

Sample Output

6.000

/* Test Instructions: There is a maze of r rows M column, starting point in [the] now to go to [r,c] for at the point [x, Y] can open a door to [x+1,y] or [x,y+1] consume 2 points mana ask how much mana can go to [r,c] analysis: Suppose Dp[i][j] is expressed at point [i , j] The average mana (expectation) required to reach [R,c] is reached from dp[i][j]: dp[i][j],dp[i+1,j],dp[i][j+1]; the corresponding probabilities are: P1,p2,p3 by E (aa+bb+cc ...) =aea+beb+cec+...//contains the expectation of the state a,b,c can be decomposed sub-expectation solution get dp[i][j]=p1*dp[i][j]+p2*dp[i+1][j]+p3*dp[i][j+1]+2;*/#include < iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <string> #include <queue> #include <algorithm> #include <map> #include <cmath> #include <iomanip> #define INF 99999999typedef long ll;using namespace Std;const int max=1000+10;int n,m;double dp[max][max],p[max][max][3]; int main () {while (~scanf ("%d%d", &n,&m)) {for (int. i=1;i<=n;++i) {for (int j=1;j<=m;++j) SC ANF ("%lf%lf%lf", &p[i][j][0],&p[i][j][1],&p[i][j][2]); } memset (dp,0,sizeof DP); for (int i=n;i>=1;--i) {for (int j=m;j>=1;--j) {if (i = = N && J= = m) continue; if (p[i][j][0] = = 1.00) continue;//This point has no way to go, the expected value is 0 (dp[i][j]=0) dp[i][j]= (p[i][j][1]* (dp[i][j+1)) +p[i][j][2]* (d P[I+1][J]) +2)/(1-p[i][j][0]); }} printf ("%.3lf\n", dp[1][1]); } return 0;}