Question connection: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3853
Loops
Time Limit: 15000/5000 MS (Java/others) memory limit: 125536/65536 K (Java/Others)
Total submission (s): 2512 accepted submission (s): 1022
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
A matrix of R * C size, from the upper left corner of the Matrix to the lower right corner. Each time you want to take a step to the right, you can stay in the same place, corresponding to three probabilities, ask about the average number of magic beans consumed. Train of Thought: Use DP [I] [J] to represent the average number of magic beans consumed from (I, j) to (R, c; it is easy to know the state transition equation: DP [I] [J] = P [I] [J] [0] * DP [I] [J] + P [I] [J] [1] * DP [i] [J + 1] + P [I] [J] [2] * DP [I + 1] [J] + 2;
#include <iostream>#include <stdio.h>#include <string.h>#include <string>#include <cstdio>#include <cmath>const int N=1100;const double eps=1e-6;using namespace std;double dp[N][N],p[N][N][3];int main(){ int r,c; while(cin>>r>>c) { for(int i=1;i<=r;i++) for(int j=1;j<=c;j++) scanf("%lf%lf%lf",&p[i][j][0],&p[i][j][1],&p[i][j][2]); memset(dp,0,sizeof(dp)); for(int i=r;i>=1;i--) for(int j=c;j>=1;j--) { if(i==r&&j==c)continue; if(fabs(1-p[i][j][0])<eps)continue; dp[i][j]=(p[i][j][1]*dp[i][j+1]+p[i][j][2]*dp[i+1][j]+2)/(1-p[i][j][0]); } printf("%.3lf\n",dp[1][1]); } return 0;}
HDU 3853 (coming soon)