首页 > 代码库 > hdu 3853 LOOPS (概率dp)

hdu 3853 LOOPS (概率dp)

/*
dp[i][j]表示(i,j)到(R,C)需要消耗的能量
则:
dp[i][j]=p1[i][j]*dp[i][j]+p2[i][j]*dp[i][j+1]+p3[i][j]*dp[i+1][j]+2;///+2 转移到下一个能量要消耗2
化简得:
dp[i][j]=((p2[i][j]*dp[i][j+1])+(p3[i][j]*dp[i+1][j])+2)/(1-p1[i][j]);
*/
# include <stdio.h>
# include <algorithm>
# include <string.h>
# include <iostream>
using namespace std;
double dp[1010][1010],p1[1010][1010],p2[1010][1010],p3[1010][1010];
int main()
{
    int r,c,i,j;
    while(~scanf("%d%d",&r,&c))
    {
        for(i=1;i<=r;i++)
        {
            for(j=1;j<=c;j++)
                scanf("%lf %lf %lf",&p1[i][j],&p2[i][j],&p3[i][j]);
        }
        dp[r][c]=0;
        for(i=r;i>=1;i--)
        {
            for(j=c;j>=1;j--)
            {
                if(i==r&&j==c)
                    continue;
                if(p1[i][j]==1)
                    continue;
                dp[i][j]=((p2[i][j]*dp[i][j+1])+(p3[i][j]*dp[i+1][j])+2)/(1-p1[i][j]);
            }
        }
        printf("%.3lf\n",dp[1][1]);
    }
    return 0;
}

hdu 3853 LOOPS (概率dp)