题意:求从(1,1)点到(r,c)的期望
dp[i][j]表示从(i,j)到(r,c)的期望,状态转移为dp[i][j] = p1*dp[i][j] + p2*dp[i][j+1] + p3*dp[i+1][j] + 2
然后化简为dp[i][j] = (2.0 + dp[i][j+1]*p[i][j][1] + dp[i+1][j]*p[i][j][2])/(1 - p[i][j][0]) 期望是从后往前推得。
注意有些点无法到达,要特判
代码入下:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<time.h>
#include<math.h>
#define N 1005
#define inf 0x7ffffff
#define eps 1e-9
#define pi acos(-1.0)
#define P system("pause")
using namespace std;
double p[N][N][3],dp[N][N];
int main()
{
ios::sync_with_stdio(false);
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
int r,c;
while(cin>>r>>c)
{
int i,j,k;
for(i = 1; i <= r; i++)
for(j = 1; j <= c; j++)
for(k = 0; k < 3; k++)
cin>>p[i][j][k];
memset(dp,0,sizeof(dp));
for(i = r; i > 0; i--)
for(j = c; j > 0; j--)
{
if(i == r && j == c)
continue;
if(fabs(1-p[i][j][0]) < eps) continue;
dp[i][j] = (2.0 + dp[i][j+1]*p[i][j][1] + dp[i+1][j]*p[i][j][2])/(1 - p[i][j][0]);
}
printf("%.3lf\n",dp[1][1]);
}
return 0;
}