HDU 3853 probability DP

Source: Internet
Author: User
Tags what magic
Loops

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

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 recommendchenyongfu conversion from http://blog.csdn.net/zhjchengfeng5/article/details/8611513 Theme

A person is trapped in a maze of R * C (2 <= r, C <= 1000). At first he is at (), and the exit of the maze is (r, c ). In each cell in the maze, he can spend two magic values to start the transfer channel. Assume that in the grid (x, y), after the transfer channel is enabled, the probability of p_lift [I] [J] being sent to (X, Y + 1 ), probability of p_down [I] [J] being delivered (x + 1, Y), and probability of p_loop [I] [J] being delivered (x, y ). Ask him what magic he expected to spend on getting to the exit.


Practice Analysis

Order:F [I] [J]It indicates the expectation of the magic value spent from the (I, j) point to the exit (R, c.

Then, we have:

F [I] [J] = 2 + p_loop [I] [J] * f [I] [J] + p_left [I] [J] * f [I] [J + 1] + p_down [I] [J] * f [I + 1] [J]

You can get the following items: (1-p_loop [I] [J]) * f [I] [J] = 2 + p_left [I] [J] (f [I] [J + 1] + p_down [I] [J] * f [I + 1] [J]

So we can roll back the recurrence.

 

 1 #include<iostream> 2 #include<cstring> 3 #include<cstdlib> 4 #include<cstdio> 5 #include<algorithm> 6 #include<cmath> 7 #include<queue> 8 #include<map> 9 10 #define N 100511 #define M 1512 #define mod 100000000713 #define mod2 10000000014 #define ll long long15 #define maxi(a,b) (a)>(b)? (a) : (b)16 #define mini(a,b) (a)<(b)? (a) : (b)17 18 using namespace std;19 20 double a[N][N][3];21 double dp[N][N];22 23 int main()24 {25     int i,j;26     int r,c;27     double p;28    // freopen("data.in","r",stdin);29     //scanf("%d",&T);30     //for(int cnt=1;cnt<=T;cnt++)31     //while(T--)32     while(scanf("%d%d",&r,&c)!=EOF)33     {34         for(i=1;i<=r;i++){35             for(j=1;j<=c;j++){36                 scanf("%lf%lf%lf",&a[i][j][0],&a[i][j][1],&a[i][j][2]);37                // printf(" %.3lf %.3lf %.3lf\n",a[i][j][0],a[i][j][1],a[i][j][2]);38             }39         }40 41         for(i=r;i>=1;i--){42             for(j=c;j>=1;j--){43                 dp[i][j]=(2+a[i][j][1]*dp[i][j+1]+a[i][j][2]*dp[i+1][j]);44                 if( (1-a[i][j][0])<1e-6){45                     dp[i][j]=0;46                 }47                 else48                     dp[i][j]/=(1-a[i][j][0]);49             }50         }51         printf("%.3f\n",dp[1][1]);52 53     }54 55     return 0;56 }

 

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.