HDU 3853 LOOPS

HDU 3853

这道题大体意思是求(1,1)到(r,c)的期望,
期望方程为 dp[x][y] = p[0] * dp[x][y] + p[1] * dp[x][y+1] + p[2] * dp[x+1][y] + 2,
其中dp[x][y]是指(x,y)到(r,c)的期望

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

const int MAXN = 1002;
double dp[MAXN][MAXN];
double p[MAXN][MAXN][3];

/*
2 2
0.00 0.50 0.50    0.50 0.00 0.50
0.50 0.50 0.00    1.00 0.00 0.00
*/

int main(){
    int r,c;
    while(~scanf("%d%d",&r,&c)){
        memset(dp,0,sizeof(dp));
        for(int x = 1;x<=r;++x){
            for(int y = 1;y<=c;++y){
                scanf("%lf%lf%lf",&p[x][y][0],&p[x][y][1],&p[x][y][2]);
            }
        }
        for(int x = r;x>0;--x){
            for(int y = c;y>0;--y){
                if(x == r && y == c) continue;
                if(p[x][y][0] == 1.00) continue;
                dp[x][y] = (p[x][y][1]*dp[x][y+1]+p[x][y][2]*dp[x+1][y]+2)/(1-p[x][y][0]);
            }
        }
        printf("%.3lf
",dp[1][1]);
    }
}
原文地址:https://www.cnblogs.com/keyi-yin/p/6724594.html