[ACM] HDU 3853 loops (probability DP, recurrence)

Source: Internet
Author: User

Loops

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 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
 
Source2011 Invitational contest host by BUPT:

There is a maze with R * C. The upper left corner () is the entrance, and the (R, c) is the exit. There are three choices for each grid. Stay in the grid and go down to the grid, two Magic values are required for each grid to go to the right. The question shows the probability of these three options for each grid and the average number of magic values consumed from the entrance to the exit.

Reverse recursion: DP [r] [c] = 0; DP [I] [J] refers to the average magic value required from the current grid to the end.

P0 [I] [J] indicates the probability that the current grid will stay in the current grid.

P1 [I] [J] indicates the probability of the next step going to the right

P2 [I] [J] indicates the probability of the next step to the left

Then DP [I] [J] = P0 [I] [J] * DP [I] [J] + P1 [I] [J] * DP [I] [J] + 1] + P2 [I] [J] * DP [I + 1] [J] + 2

That is, DP [I] [J] = (P1 [I] [J] * DP [I] [J + 1] + P2 [I] [J] * DP [I + 1] [J] + 2) /(1-p0 [I] [J]);

Code:

# Include <iostream> # include <stdio. h> # include <cmath> # include <iomanip> # include <string. h> using namespace STD; const int maxn = 1010; const double EPS = 1e-6; double DP [maxn] [maxn]; double P0 [maxn] [maxn]; double P1 [maxn] [maxn]; double P2 [maxn] [maxn]; int R, C; int main () {While (CIN> r> C) {for (INT I = 1; I <= r; I ++) for (Int J = 1; j <= C; j ++) scanf ("% lf", & P0 [I] [J], & P1 [I] [J], & p2 [I] [J]); for (INT I = r; I> = 1; I --) for (Int J = C; j> = 1; j --) {If (FABS (1-p0 [I] [J]) <= EPS) // some grids can only be in their own positions, and cannot move DP [I] [J] = 0; else DP [I] [J] = (P1 [I] [J] * DP [I] [J + 1] + P2 [I] [J] * DP [I + 1] [J]) + 2)/(1-p0 [I] [J]);} cout <setiosflags (IOs: fixed) <setprecision (3) <DP [1] [1] <Endl;} return 0 ;}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.