HDU 3853 loops (probability DP)

Source: Internet
Author: User
Tags define local
Loops

Time Limit: 15000/5000 MS (Java/others) memory limit: 125536/65536 K (Java/others) total submission (s): 2552 accepted submission (s): 1041

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 exact CT magic power homura need to escape from the loops. sample input2 20.00 0.50 0.50 0.50 0.500.50 0.00 0.50 0.00 1.00 0.00 sample output6.000 source 0.00 Invitational contest host by BUPT question: there is a lattice of R * C, starting from the lattice (2011, you can maintain three positions, stay in the same position, step to the right, go to the next step, and give the corresponding probability. Each step consumes 2 magic values, and finally reaches (R, c) average magic value and question: We know the probability of E (I) = p1 + p2 + p3 ...... + PN; For E (AX + bk) = A * Ex + B * ek; we may consider starting from (R, c): The first thing to be clear is: you cannot stop until (R, c) is reached. Therefore, DP [1] [1] = DP [1] [2] * mat [1] [1] [3] + dp [2] [1] * mat [1] [1] [2]; DP [1] [1] = DP [1] [1]/(1-map [1] [1] [0]) // remove other probabilities of stopping, the expected push is as follows: DP [I] [J] = DP [I] [J + 1] * mat [I] [J] [3] + dp [I + 1] [J] * mat [I] [J] [2]; DP [I] [J] = DP [I] [J]/(1-map [I] [J] [0]) // remove the probability of stopping, expected, but due to this inference from (), there will be no convergence problem; Code:
// # Define local # include <stdio. h> # include <string. h> # include <stdlib. h> # define maxn 1001 double DP [maxn] [maxn]; double map [maxn] [maxn] [3]; int main () {int RR, CC; # ifdef local freopen ("test. in "," r ", stdin); # endif while (scanf (" % d ", & RR, & CC )! = EOF) {for (INT I = 1; I <= RR; I ++) for (Int J = 1; j <= cc; j ++) scanf ("% lf", & map [I] [J] [0], & map [I] [J] [1], & map [I] [J] [2]); memset (DP, 0, sizeof (DP); For (INT I = RR; I> 0; I --) for (Int J = cc; j> 0; j --) {if (I = RR & J = cc) continue; // (RR, CC) this point does not need to be taken at the exit. It is stopped in the same place if (Map [I] [J] [0]! = 1.0) // if the end point is not reached, the question will be misunderstood! {DP [I] [J] = DP [I] [J] * map [I] [J] [0] + dp [I + 1] [J] * map [i] [J] [2] + dp [I] [J + 1] * map [I] [J] [1] + 2; DP [I] [J]/= (1.0-map [I] [J] [0]);} printf ("%. 3lf \ n ", DP [1] [1]);} return 0 ;}

 

HDU 3853 loops (probability DP)

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.