【动态规划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]);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章