HDU 3853 LOOPS(概率DP)

题目大意:一个r*c的矩阵,给出了每个留在原来格子和往左往右一个格子的概率,求从(1,1)到(r,c)的期望。

注意一个p1=1这样的点的特殊情况。http://www.cnblogs.com/kuangbin/archive/2012/10/03/2711140.html

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define esp 1e-5
#define maxn 1005
double c1[maxn][maxn];
double c2[maxn][maxn];
double c3[maxn][maxn];
double e[maxn][maxn];
int main()
{
    int r,c;
    while(scanf("%d%d",&r,&c)!=EOF)
    {
        memset(e,0,sizeof(e));
    for(int i=1;i<=r;i++)
        for(int j=1;j<=c;j++)
    {
        scanf("%lf%lf%lf",&c1[i][j],&c2[i][j],&c3[i][j]);
    }
    for(int i=r;i>=1;i--)
        for(int j=c;j>=1;j--)
    {
        if(i==r&&j==c) continue;
        if(fabs(1-c1[i][j])<esp) continue;
        e[i][j]=(c2[i][j]*e[i][j+1]+c3[i][j]*e[i+1][j]+2)/(1-c1[i][j]);
      //  cout<<e[i][j]<<endl;
    }
    printf("%.3f
",e[1][1]);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/Twsc/p/6705936.html