Title:
Given an n * m lattice, a person starts at (1,1) and has to go to (n,m).
Suppose at (x,y), every time she can spend 2 magic energy, the probability of p1 goes to (x,y), the probability of p2 to go to (x,y+1), the probability of p3 to go to (x +1,y), guaranteeing p1+p2+p3 = 1.
Ask what is the expected cost of walking from (1,1) to (n,m).
Ideas:
Short answer probability dp, inverse.
dp[i][j] = (dp[i][j+1] * p2 + dp[i+1][j] * p3) / (1 - p1)。
But pay attention to skip when p1 == 1 .
Code:
1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 using namespace std; 5 const int N = 1005; 6 double dp[N][N]; 7 double a[N][N][3]; 8 int main() 9 { 10 int n,m; 11 while (scanf("%d%d",&n,&m) != EOF) 12 { 13 memset(dp,0,sizeof(dp)); 14 for (int i = 1;i <= n;i++) 15 { 16 for (int j = 1;j <= m;j++) 17 { 18 for (int k = 0;k < 3;k++) 19 { 20 scanf("%lf",&a[i][j][k]); 21 } 22 } 23 } 24 dp[n][m] = 0; 25 for (int i = n;i >= 1;i--) 26 { 27 for (int j = m;j >= 1;j--) 28 { 29 if (i == n && j == m) continue; 30 if (fabs(1.0-a[i][j][0]) < 1e-8) continue; 31 dp[i][j] = (a[i][j][1] * dp[i][j+1] + a[i][j][2] * dp[i+1][j] + 2.0) / (1.0-a[i][j][0]); 32 } 33 } 34 printf("%.3f\n",dp[1][1]); 35 } 36 return 0; 37 }