HDU 3853 LOOPS (probabilistic DP seeking expectation)

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=3853


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

Source2011 Invitational Contest Host by Bupt

Test instructions

A person walks to the lower-right corner from the upper-left corner of a rectangle that is a row C column of R!

Each point has one if there is a certain probability to transmit to another point separately!

Ask for the expectations she needs!

The code is as follows:

#include <cstdio> #include <cstring>double dp[1017][1017];d ouble a[1017][1017][4];//dp[i][j] = dp[i][j]*a I [j] [1]+dp[i][j+1]*a[i][j][2]+dp[i+1][j]*a[i][j][3]+2;//consumes two mana value per step so add 2//to move: dp[i][j] = (dp[i][j+1]*a[i][j][2]+dp[i    +1][j]*a[i][j][3]+2)/(1.0-a[i][j][1]); int main () {int r, C; while (~SCANF ("%d%d", &r,&c)) {for (int i = 1; I <= R; i++) {for (int j = 1; J <= C J + +) {for (int k = 1; k <= 3; k++) {scanf ("%lf", &a[i][                J][k]);        }}} memset (DP, 0,sizeof (DP));        Dp[r][c] = 0; for (int i = r, I >= 1; i--) {for (int j = C; j >= 1; j--) {if (I==r &amp                ;& j==c) {continue;                } if (a[i][j][1] = = 1) {continue;//must be in situ} DP[I][J] = (Dp[i][j+1]*a[i][j]            [2]+dp[i+1][j]*a[i][j][3]+2)/(1.0-a[i][j][1]);    }} printf ("%.3lf\n", dp[1][1]); } return 0;}


HDU 3853 LOOPS (probabilistic DP seeking expectation)

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.