HDU 3853 LOOPS(概率DP)

本文介绍了一种利用全概率期望公式解决动态规划问题的方法,具体目标是从坐标(1,1)出发到达坐标(n,m),每次决策消耗2点能量,可以通过原地不动、向右或向下移动,每种移动方式都有特定的概率。通过递归定义状态转移方程并进行计算,最终得出从起始点到终点的最小能量消耗路径。代码示例展示了实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:点击打开链接

题意:求从(1, 1)点走到(n, m)点的花费能量的期望, 每次决策消耗2点能量。 每次可以原地不动或者向右或者向下, 分别有个概率。

思路:运用全概率期望公式, d[i][j] = a[1]*d[i][j] + a[2]*d[i+1][j] + a[3]*d[i][j+1] + 2, 其中a[i]是三个可能情况的概率。  因为dp方程要满足无后效性, 所以移项得d[i][j] = (2 + a[2]*d[i+1][j] + a[3]*d[i][j+1]) / (1 - a[i][j])。

细节参见代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
typedef long double ld;
const ld eps = 1e-9, PI = 3.1415926535897932384626433832795;
const int mod = 1000000000 + 7;
const int INF = 0x3f3f3f3f;
// & 0x7FFFFFFF
const int seed = 131;
const ll INF64 = ll(1e18);
const int maxn = 1000 + 10;
int T,n,m,vis[maxn][maxn],kase = 0;
double d[maxn][maxn];
struct node {
    double a, b, c;
}a[maxn][maxn];
double dp(int r, int c) {
    double& ans = d[r][c];
    if(r == n && c == m) return 0;
    if(vis[r][c] == kase) return ans;
    vis[r][c] = kase;
    if(fabs(a[r][c].a - 1) < eps) return 0;
    ans = 0.0;
    if(r < n) ans += dp(r+1, c) * a[r][c].c;
    if(c < m) ans += dp(r, c+1) * a[r][c].b;
    ans += 2;
    ans /= (1 - a[r][c].a);
    return ans;
}
int main() {
    while(~scanf("%d%d",&n,&m)) {
        for(int i=1;i<=n;i++) {
            for(int j=1;j<=m;j++) {
                scanf("%lf%lf%lf",&a[i][j].a,&a[i][j].b,&a[i][j].c);
            }
        }
        ++kase;
        double ans = dp(1, 1);
        printf("%.3f\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值