hdu 3853

hdu 3853

Humora 被困在一个r * c 个小房间组成的矩形迷宫里,起点位位置在(1,1)终点位置在(r, c);每次可以花费两点法力从房间(i, j)以一定的概率跑到(i+1, j)房间 或者(i,j+1)房间或者回到原来的房间。求Humora逃出去的要花费法力的期望。

求期望的概率dp。

设dp[i][j] 表示在房间(i, j)逃出去的花费法力的期望。显然dp[1][1]为题目所求

对于每个房间可能有三种情况分别是移动到房间 (i,j) (i, j+1) (i+1,j) 。可以列出dp方程:

dp[i][j] = p[i][j].a * dp[i][j] + p[i][j].b * dp[i][j+1] + p[i][j].c * dp[i+1][j]; (dp[r][c] = 0.0)

写代码时要注意边界。

还有就是题目说解不小于
1000000  那么说明一定存在解,所以题目数据出现的 p[i][j].a = 1的情况要排除掉。

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

struct Node {
    double a, b, c;
    Node(){}
    Node(double _a, double _b, double _c)
        :a(_a),b(_b),c(_c){}
};

int r, c;

Node p[1010][1010];

double dp[1010][1010];

double DP(int x, int y) {
    if (dp[x][y] != -1.0) return dp[x][y];
    if (x == r && y == c) return 0.0;
    if (abs(1 - p[x][y].a) < 1e-8) return dp[x][y] = 1.0;
    double res = 0.0;
    if (y != c) res += p[x][y].b * DP(x, y+1);
    if (x != r) res += p[x][y].c * DP(x+1, y);
    return dp[x][y] = (res + 2.0) * 1.0 / (1.0 - p[x][y].a);
}

int main () {
    while (cin >> r >> c) {
        double _a, _b, _c;
        for (int i=1; i<=r; i++) {
            for (int j=1; j<=c; j++) {
                scanf("%lf %lf %lf", &_a, &_b, &_c);
                p[i][j] = Node(_a, _b, _c);
                dp[i][j] = -1.0;
            }
        }
        printf ("%.3lf\n", DP(1, 1));
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-01 17:06:54

hdu 3853的相关文章

概率dp HDU 3853

H - LOOPS Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 3853 Appoint description:  System Crawler  (2014-10-22) Description Akemi Homura is a Mahou Shoujo (Puella Magi/Magical Girl). Homura wa

hdu 3853 概率DP 简单

http://acm.hdu.edu.cn/showproblem.php?pid=3853 题意:有R*C个格子,一个家伙要从(0,0)走到(R-1,C-1) 每次只有三次方向,分别是不动,向下,向右,告诉你这三个方向的概率,以及每走一步需要耗费两个能量,问你走到终点所需要耗费能量的数学期望: 回头再推次,思想跟以前的做过的类似 注意点:分母为0的处理 #include <cstdio> #include <cstring> #include <algorithm>

HDU 3853 概率dp

LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Total Submission(s): 2337    Accepted Submission(s): 951 Problem Description Akemi Homura is a Mahou Shoujo (Puella Magi/Magical Girl).Homura wants to help he

[ACM] hdu 3853 LOOPS (概率DP,递推)

LOOPS 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

HDU 3853:LOOPS

LOOPS 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3853 题意: 有一个网格,从点(1,1)开始走,要走到点(n,m),点(X,Y)可以走到点(X,Y),(X,Y+1),(X+1,Y),概率分别为p0,p1,p2,每走一步耗费2点魔力,求走到终点所耗费的魔力的期望 题解: 一道简单的求期望的题,不会求期望的可以看下这里 具体公式如下: dp[i][j]=p[i][j][0]*dp[i][j]+p[i][j][1]*dp[i][j+1]+

HDU 3853 LOOPS (概率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

HDU 3853 LOOPS(概率dp求期望啊)

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

hdu 3853 LOOPS(期望)

http://acm.hdu.edu.cn/showproblem.php?pid=3853 求从[1,1]到[r,c]的所花power的期望,每走一步消耗的power是2,给出从[i,j]到[i,j],[i,j+1],[i+1][j]概率. dp[i][j]表示从[i,j]到[r,c]的消耗power的期望,已知终态dp[r][c] = 0,然后逆推. 很难想的是当在原地的概率为1时,走不到[r,c],状态转移方程中结果是INF,与题目要求相矛盾. #include <stdio.h> #i

hdu 3853 LOOPS 动态规划

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3853 迷宫类的动态规划 首先要作个数学推导 假设留在原地.右移.下移的概率分别是a, b, c 用dp[i][j]表示在第i行第j格能走出去的期望步数 则有: dp[i][j] = a * (dp[i][j] + 1) + b * (dp[i][j+1] + 1) + c * (dp[i+1][j] + 1) 整理一下可得: dp[i][j] = 1/(1-a) * (a +  b * (dp[i]