Loops
Time Limit: 15000/5000 MS (Java/others) memory limit: 125536/65536 K (Java/Others)
Total submission (s): 649 accepted submission (s): 267
Problem descriptionakemi homura is a Mahou shoujo (puella Magi/magical girl ).
Homura wants to help her friend Madoka Save the world. But because of the plot of the boss incubator, she is trapped in a labyrinth called loops.
The planform of the loops is a rectangle of R * C grids. there is a portal in each grid does t the exit grid. it costs homura 2 magic power to use a portal once. the portal in a grid G (R, c) will send homura to the grid below g (grid (R + 1, C )), the grid on the Right of g (grid (R, C + 1), or even g itself at respective probability (how edevil the boss incubator is )!
At the beginning homura is in the top left corner of the loops (1, 1), and the exit of the labyrinth is in the bottom right corner (r, c )). given the probability of transmissions of each portal, your task is help poor homura calculate the specific CT magic power she need to escape from the loops.
Inputthe first line contains two 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. two groups of numbers are separated by 4 spaces.
It is ensured that the sum of three numbers in each group is 1, and the second numbers of the rightmost groups are 0 (as there are no grids on the right of them) while the third numbers of the downmost groups are 0 (as there are no grids below them ).
You may ignore the last three numbers of the input data. They are 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 specific CT magic power homura need to escape from the loops.
Sample input2 2 0.00 0.50 0.50 0.50 0.00 0.50 0.50 0.50 0.00 1.00 0.00 0.00
Sample output6.000
Source2011 Invitational contest host by BUPT
Recommendchenyongfu
Relatively simple probability DP. It is easy to write the equation to deliver the answer.
Pay attention to special cases.
/* HDU 3853 resolution: Set DP [I] [J] to indicate the amount of energy consumed from (I, j) to (R, C: DP [I] [J] = P1 [I] [J] * DP [I] [J] + P2 [I] [J] * DP [I] [J + 1] + P3 [I] [J] * DP [I + 1] [J] + 2; simplified to: DP [I] [J] = P2 [I] [J] * DP [I] [J + 1]/(1-p1 [I] [J]) + P3 [I] [J] * DP [I + 1] [J]/(1-p1 [I] [J]) + 2/(1-p1 [I] [J]). Note that P1 [I] [J] = 1. The answer is only to ensure that the answer is less than 1000000, but some points may never be reached. Therefore, P1 [I] [J] is allowed. Otherwise, it will be wa. */ # Include <Stdio. h> # Include <Iostream> # Include <Algorithm> # Include < String . H> # Include <Math. h> Using Namespace STD; Const Int Maxn = 1010 ; Const Double EPS = 1E- 5 ; Double DP [maxn] [maxn]; Double P1 [maxn] [maxn]; Double P2 [maxn] [maxn]; Double P3 [maxn] [maxn]; Int Main (){ Int R, C; While (Scanf ( " % D " , & R, & C )! = EOF ){ For ( Int I = 1 ; I <= r; I ++ ) For ( Int J = 1 ; J <= C; j ++ ) Scanf ( " % Lf " , & P1 [I] [J], & p2 [I] [J], & P3 [I] [J]); DP [r] [C] = 0 ; For ( Int I = r; I> = 1 ; I -- ) For ( Int J = C; j> = 1 ; J -- ){ If (I = R & J = C) Continue ; If (FABS ( 1 -P1 [I] [J]) <EPS) Continue ; DP [I] [J] = P2 [I] [J]/( 1 -P1 [I] [J]) * DP [I] [J + 1 ] + P3 [I] [J]/(1 -P1 [I] [J]) * DP [I + 1 ] [J] + 2 /( 1 - P1 [I] [J]);} printf ( " %. 3lf \ n " , DP [ 1 ] [ 1 ]);} Return 0 ;}