HDU 3853 LOOPS (probability dp)
LOOPS
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others)
Total Submission (s): 2931 Accepted Submission (s): 1209
Problem 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 tZ role? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> crop + Cjxicj4KPGJyPgo8YnI + crop =" brush: java; "> 2 20.00 0.50 0.50 0.50 0.00 0.500.50 0.50 0.00 1.00 0.00Sample Output
6.000
Source 2011 Invitational Contest Host by BUPT
Question link: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 3853
A r * c maze, each of which is transmitted to the grid (r, c), grid (r, c + 1 ), the probability of grid (r + 1, c), each time it takes 2 magic values, from the starting point (1, 1) to the end point (r, c) magic values expected
Question Analysis: simple probability dp, using p [0] [I] [j], p [1] [I] [j], p [2] [I] [j] indicates three corresponding probabilities of each vertex, respectively. dp [I] [j] indicates from (I, j) to (r, c) expected magic value, you can get the transfer equation:
Dp [I] [j] = dp [I] [j] * p [0] [I] [j] + dp [I] [j + 1] * p [1] [I] [j] + dp [I + 1] [j] * p [2] [I] [j]
==> Dp [I] [j] = (dp [I] [j + 1] * p [1] [I] [j] + dp [I + 1] [j] * p [2] [I] [j]) // (1.0-p [0] [I] [j])
From this transfer equation, it is not difficult to see that dp [r] [c] can be pushed forward. dp [r] [c] = 0, note that the denominator is 0. If p [0] [I] [j] = 1, it is obviously an endless loop, dp [I] [j] = 0
#include
#include
int const MAX = 1005;double const EPS = 1e-10;double p[3][MAX][MAX], dp[MAX][MAX];int main(){ int r, c; double p1, p2, p3; while(scanf("%d %d", &r, &c) != EOF) { for(int i = 1; i <= r; i++) for(int j = 1; j <= c; j++) scanf("%lf %lf %lf", &p[0][i][j], &p[1][i][j], &p[2][i][j]); dp[r][c] = 0; for(int i = r; i >= 1; i--) for(int j = c; j >= 1; j--) if(!(i == r && j == c)) if(fabs(p[0][i][j] - 1.0) < EPS) dp[i][j] = 0; else dp[i][j] = (dp[i][j + 1] * p[1][i][j] + dp[i + 1][j] * p[2][i][j] + 2) / (1.0 - p[0][i][j]); printf("%.3f\n", dp[1][1]); }}