天天看點

hdu3853之機率dp入門 LOOPS

time limit: 15000/5000 ms (java/others)    memory limit: 125536/65536 k (java/others)

total submission(s): 1651    accepted submission(s): 653

problem description

akemi 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.

hdu3853之機率dp入門 LOOPS

the planform of the loops is a rectangle of r*c grids. there is a portal in 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) 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 evil 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 expect magic power

she need to escape from the loops.

input

the 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

output

a real number at 3 decimal places (round to), representing the expect magic power homura need to escape from the loops.

sample input

sample output