HDU 3853 LOOPS

来源:互联网 发布:mac os x 10.11.6 u盘 编辑:程序博客网 时间:2024/05/17 20:29

LOOPS

期望DP。题目意思很容易理解,这里就不再解释了。方程dp[i][j]表示从i,j点出发到达(R , C)的消耗能量的期望值。题目给了状态转移的几个方向,dp[i][j] = x1 * dp[i][j] + x2 * dp[i][j + 1] + x3 * dp[i + 1][j] + 2 ; 这里x1,x2,x3即为从(i,j)走到(i,j),(i , j + 1) , (i + 1 , j)的概率,由于打开一扇门还需要2个能量值。然后整理一下。这里需要注意的是存在某种情况:x1 = 1 ,这时我们需要特判一下。
不好意思。。之前由于贴代码不慎。。贴了错误的上去。。。。现在特改正。。。尴尬


/*    author   : csuchenan    prog     : hdu 3853    algorithm: DP*/#include <cstdio>#include <cmath>const int maxn = 1005 ;const double eps = 1e-5 ;struct Prob{    double x1 , x2 , x3 ;}prob[maxn][maxn] ;double dp[maxn][maxn] ;int main(){    int R , C ;    while(scanf("%d%d", &R , &C) != EOF){       for(int i = 1 ; i <= R ; i ++){            for(int j = 1 ; j <= C ; j ++){                scanf("%lf%lf%lf" , &prob[i][j].x1 , &prob[i][j].x2                , &prob[i][j].x3) ;            }       }       dp[R][C] = 0 ;       for(int i = R ; i >= 1 ; i --){            for(int j = C ; j >= 1 ; j --){                if(i == R && j == C)                    continue ;                if(fabs(1 - prob[i][j].x1) < eps)                    continue ;                double x1 , x2 , x3 ;                x1 = prob[i][j].x1 ;                x2 = prob[i][j].x2 ;                x3 = prob[i][j].x3 ;                dp[i][j] = (x2 * dp[i][j + 1] + x3 * dp[i + 1][j] + 2)                /(1 - x1) ;            }       }       printf("%.3f\n" , dp[1][1]) ;    }    return 0 ;}

原创粉丝点击