HDU3853-LOOPS (expectation of probability DP)

Source: Internet
Author: User
Loops Time Limit: 15000/5000 MS (Java/others) memory limit: 125536/65536 K (Java/Others)
Total submission (s): 1864 accepted submission (s): 732


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
 
There are R * C lattice, a guy from (0, 0) to (R-1, C-1) each time only three directions, are not moving, down, to the right, it tells you the probability of these three directions, and it consumes two energy each step, and asks you the mathematical expectation of energy consumption to reach the end point:
Idea: Very bare probability DP expectation, but there is a point to note, that is, when the denominator is 0, the floating point will only appear Nan, rather than re as the integer!
#include <iostream>#include <cstdio>#include <cmath>#include <cstring>using namespace std;const int maxn =  1000+10;const int dx[3] = {0,0,1};const int dy[3] = {0,1,0};const double eps = 1e-8;double p[maxn][maxn][3];int r,c;double dp[maxn][maxn];bool isok(int x,int y){    return x>=0&&x<r && y>=0&&y <c &&!(x==r-1&&y==c-1);}int main(){    while(~scanf("%d%d",&r,&c)){        for(int i = 0; i < r; i++){            for(int j = 0; j < c; j++){                for(int k = 0; k < 3; k++){                    scanf("%lf",&p[i][j][k]);                }            }        }        dp[r-1][c-1] = 0.0;        for(int i = r-1; i >= 0; i--){            for(int j = c-1; j >= 0; j--){                double t = 2.0;                for(int k = 1; k < 3; k++){                    int xx = i + dx[k];                    int yy = j + dy[k];                    if(isok(xx,yy)){                        t += dp[xx][yy]*p[i][j][k];                    }                }                if(fabs(1-p[i][j][0])<eps) dp[i][j] = 0;                else dp[i][j] = t/(1-p[i][j][0]);            }        }        printf("%.3lf\n",dp[0][0]);    }    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.