【动态规划09】hdu3853 LOOPS(期望dp)

题目描述

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.

总而言之就是一个r*c的矩阵,每次移动的cost是2,每次可以向右向下或者不动给你三种情况的概率,求从
(1,1)到(r,c)的最小期望膜力。
很容易想到设f[i][j]表示从(i,j)到(r,c)所需要的膜力期望。
f[i][j]一共可以到达f[i+1][j],f[i][j+1]和f[i][j]。
易得到递推式f[i][j]=(2+f[i][j])*mp[i][j][0]+(2+f[i][j+1]*mp[i][j][1]+(2+f[i+1][j])*mp[i][j][2]
移向得到f[i][j]= 2(mp[i][j][0]+mp[i][j][1]+mp[i][j][2])+(2+f[i][j+1]mp[i][j][1]+(2+f[i+1][j])mp[i][j][2]1mp[i][j][0]
需要注意的是特殊判断分母不能为0

#include<bits/stdc++.h>
#define fer(i,j,n) for(int i=j;i<=n;i++)
#define far(i,j,n) for(int i=j;i>=n;i--)
#define ll long long
const int maxn=1010;
const int INF=1e9+7;
using namespace std;
/*----------------------------------------------------------------------------*/
inline int read()
{
    char ls;int x=0,sng=1;
    for(;ls<'0'||ls>'9';ls=getchar())if(ls=='-')sng=-1;
    for(;ls>='0'&&ls<='9';ls=getchar())x=x*10+ls-'0';
    return x*sng;
}
/*----------------------------------------------------------------------------*/
double mp[maxn][maxn][3],f[maxn][maxn];
int n,m;
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        fer(i,1,n)
            fer(j,1,m)
                fer(k,0,2)
                    scanf("%lf",&mp[i][j][k]);
        f[n][m]=0;
        far(i,n,1)
            far(j,m,1)
            {
                if(i==n&&j==m)continue;
                if(mp[i][j][0]==1)continue;
                f[i][j]=(2+f[i][j+1]*mp[i][j][1]+f[i+1][j]*mp[i][j][2])/(1-mp[i][j][0]);  
            }
    printf("%.3lf\n",f[1][1]);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值