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][j+1] + 1) + c * (dp[i+1][j] + 1))

然后有一个神坑我至今觉得是题目有问题

题目保证答案不大于一百万 但是却有那种停留在原地的概率为1的“死房间”

按照数学期望的算法 如果在除终点外的地方出现“死房间” 那期望的步数就应该变得无限大才对

所以我就以为题目保证答案不大于一百万是在暗示不会发生这种情况【打脸TAT

然后看了题解才发现 如果遇到死房间就直接跳过 死房间的步数期望是0 这不是强行解释吗...

各位千万记得绕开这个毫无意义的坑点

#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <stack>
#include <set>
#include <queue>
#include <vector>

using namespace std;

const int maxn = 1010;
const double eps = 1e-7;

double dp[maxn][maxn];
double a[maxn][maxn], b[maxn][maxn], c[maxn][maxn];

int main()
{
    //freopen("in.txt", "r", stdin);

    int n, m;
    while(scanf("%d%d", &n, &m) == 2)
    {
        memset(dp, 0, sizeof(dp));

        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= m; j++)
            {
                scanf("%lf%lf%lf", &a[i][j], &b[i][j], &c[i][j]);
            }
        }

        for(int i = n; i >= 1; i--)
        {
            for(int j = m; j >= 1; j--)
            {
                if(i == n && j == m)
                    dp[i][j] = 0;
                else if(fabs(a[i][j] - 1.0) < eps)
                    continue;
                else
                    dp[i][j] = (1.0 / (1 - a[i][j])) * (a[i][j] + b[i][j] * (dp[i][j+1]+1) + c[i][j] * (dp[i+1][j]+1));
            }
        }

        double ans = 2.0 * dp[1][1];
        printf("%.3f\n", ans);

    }

    return 0;
}
时间: 2024-10-13 08:43:13

hdu 3853 LOOPS 动态规划的相关文章

[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 (概率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(概率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:期望dp【网格型】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3853 题意: 有一个n*m的网格. 给出在每个格子时:留在原地.向右走一格,向下走一格的概率. 每走一格会消耗2点体力. 问你从(1,1)到达终点(n,m)消耗体力的期望. 题解: 表示状态: dp[i][j] = rest steps(剩余路程花费体力的期望) i,j:现在的位置 找出答案: ans = dp[0][0] 如何转移: 期望dp的套路:考虑子期望... now: dp[i][j] 能

hdu 3853 LOOPS(概率 dp 期望)

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

hdu 3853 LOOPS DP

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

hdu 3853 LOOPS 【概率DP】

题目链接:http://acm.acmcoder.com/showproblem.php?pid=3853 题意:求走到终点消耗能量的期望. 解法: dp[i][j] 表示走到 i行j列 的期望. dp[i][j] 可以转移到 dp[i][j+1] 和 dp[i+1][j] 和 dp[i][j] 各个转移的概率已经给出,由dp[n][m] == 0倒推即可.答案为dp[1][1] 代码: #include <stdio.h> #include <string.h> #include