http://acm.split.hdu.edu.cn/showproblem.php?pid=3853
LOOPS
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 except 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 evil 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 EXPECT magic power she need to escape from the LOOPS.
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
题意:每一个格子有三个概率,分别是原地不动的概率,走到(i,j+1)的概率,走到(i+1,j)的概率,保证在边界的时候相对的概率为0,求从(1,1)走到(r,c)的期望。
思路:居然连圆神的题目都能出Orz,注意一个如果原地不动的概率为1要跳过。
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
#define N 1010 double dp[N][N];
double maze[N][N][]; int main()
{
int r, c;
scanf("%d%d", &r, &c);
while(~scanf("%d%d", &r, &c)) {
for(int i = ; i <= r; i++) {
for(int j = ; j <= c; j++) {
for(int k = ; k < ; k++) {
scanf("%lf", &maze[i][j][k]);
}
}
}
dp[r][c] = ;
for(int i = r; i > ; i--) {
for(int j = c; j > ; j--) {
if(j == c && i == r) continue;
if(maze[i][j][] == ) continue; // 坑点,如果为1的话会永远无法走出去
dp[i][j] = (maze[i][j][] * dp[i][j+] + maze[i][j][] * dp[i+][j] + ) / ((double) - maze[i][j][]);
}
}
printf("%.3f\n", dp[][]);
}
return ;
}