(概率dp/期望)LOOPS

http://acm.hdu.edu.cn/showproblem.php?pid=3853
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 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.

题意为位于一个迷宫,刚开始在左上角(1,1),每个格子都有p1的概率留在原地,p2的概率向右移动一个,p3的概率向下移动一格,但都要花费2点魔力值。求到右下角(r,c)的花费魔力值期望
dp[i][j]=p1*dp[i][j]+p2*dp[i][j+1]+p3*dp[i+1][j]+2;
注意有的格子p1为1的时候,这个格子是不能到的,他一直在原地,我们不处理它并且把他的期望置为0,不对其他格子产生影响

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

const int maxn = 1000 + 5;
const double eps = 1e-5;
double dp[maxn][maxn];
struct p {
    double p1, p2, p3;
} P[maxn][maxn];
int main()
{
    int r, c;
    while(~scanf("%d%d", &r, &c)) {
        for(int i = 1; i <= r; ++i)
            for(int j = 1; j <= c; ++j)
                scanf("%lf%lf%lf", &P[i][j].p1, &P[i][j].p2, &P[i][j].p3);
        dp[r][c] = 0;
        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].p1) < eps) {
                    dp[i][j] = 0;
                    continue;
                }
                dp[i][j] = P[i][j].p2 * dp[i][j + 1] + P[i][j].p3 * dp[i + 1][j] + 2.0;
                dp[i][j] /= (1 - P[i][j].p1);
            }
        }
        printf("%.3f\n", dp[1][1]);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值