标签:
解题思路:
求期望,用逆推。
用dp[i][j]表示在点(i,j)到终点所需要的期望能量,转移公式为:
dp[i][j] = p1[i][j] * dp[i][j] + p2[i][j] * dp[i][j+1] + p3[i][j] * dp[i+1][j] + 2;
化简得:
dp[i][j] = (p2[i][j] * dp[i][j+1] + p3[i][j] * dp[i+1][j] + 2) / (1 - p1[i][j]);
要注意存在p1[i][j] == 1的情况。
#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <stack> #include <queue> #define LL long long #define FOR(i,x,y) for(int i=x;i<=y;i++) using namespace std; const int maxn = 1000 + 10; const double eps = 1e-5; double dp[maxn][maxn], p1[maxn][maxn], p2[maxn][maxn], p3[maxn][maxn]; int n, m; int main() { while(scanf("%d%d", &n, &m)!=EOF) { FOR(i,1,n) FOR(j,1,m) scanf("%lf%lf%lf", &p1[i][j], &p2[i][j], &p3[i][j]); memset(dp, 0, sizeof(dp)); for(int i=n;i>=1;i--) { for(int j=m;j>=1;j--) { if(i == n && j == m) continue; if(fabs(1-p1[i][j]) < eps) 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; }
标签:
原文地址:http://blog.csdn.net/moguxiaozhe/article/details/43528145