hdu 3853 LOOPS 递推求期望

hdu 3853 LOOPS

计算期望,往往是从末状态到初状态倒推的,相见恨晚

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
const int MAXN=1010;
const double eps=1e-5;
double dp[MAXN][MAXN];
double p1[MAXN][MAXN];
double p2[MAXN][MAXN];
double p3[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",&p1[i][j],&p2[i][j],&p3[i][j]);
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-p1[i][j])<eps)continue;
dp[i][j]=p2[i][j]/(1-p1[i][j])*dp[i][j+1]+p3[i][j]/(1-p1[i][j])*dp[i+1][j]+2/(1-p1[i][j]);
}
printf("%.3lf\n",dp[1][1]);
}
return 0;
}