HDU 3853 loops probability DP

Source: Internet
Author: User
E-Loops Time limit:5000 Ms Memory limit:65536kb 64bit Io format:% I64d & % i64usubmit status practice HDU 3853 appoint description: System crawler)

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.

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.




 

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 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 
Xiao meiyan needs to go from () to (n, n) at each point. There are three options that may not go or go to the right or down, and two magic values will be spent each time.
Expectation for the magic value spent: Expectation indicates that the next state of each vertex may be the right or the following: DP [I] [J] = (DP [I] [J] + 1] * probability on the right + dp [I + 1] [J] * probability + cost) /(right + probability below) accode:
#include <cstdio>#include <cstring>#include <cmath>#define maxn 1002using namespace std;double dp[maxn][maxn],a[maxn][maxn],b[maxn][maxn],c[maxn][maxn];int main(){    int n,m;    while(~scanf("%d%d",&n,&m)){        memset(dp,0,sizeof(dp));        for(int i=1;i<=n;++i)for(int j=1;j<=m;++j)scanf("%lf%lf%lf",&a[i][j],&b[i][j],&c[i][j]);        for(int i=n;i>=1;--i)for(int j=m;j>=1;--j)            if((i==n&&j==m)||(fabs(1-a[i][j])<1e-7))continue;            else dp[i][j]=(dp[i][j+1]*b[i][j]+dp[i+1][j]*c[i][j]+2)/(b[i][j]+c[i][j]);        printf("%.3lf\n",dp[1][1]);    }}/*2 20.00 0.50 0.50    0.50 0.00 0.500.50 0.50 0.00    1.00 0.00 0.00*/


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.