E[n][m] = 0;

三种运动的概率分别是a,b,c

E[i][j] = a*E[i][j] + b*E[i][j+1] + c*E[i+1][j]

->E[i][j] = ( b*E[i][j+1] + c*E[i+1][j] )/(1-a)

如果1-a近似到0,那么说明只能到它本身,所以它不能到达终点,所以它的期望是无穷,也就是不能到达,要进行特判,赋值为0

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#define MAX 1007
#define eps 1e-8

using namespace std;

double dp[MAX][MAX];
double p[MAX][MAX][4];

int n,m;

void dfs ( int x , int y )
{
    if ( x== n && y == m )
    {
        dp[x][y] = 0.0;
        return;
    }
    if ( dp[x][y] > -0.5 ) return;
    dp[x][y] = 2.0;
    if ( y < m && dp[x][y+1] < -0.5 ) dfs ( x , y+1 );
    dp[x][y] += dp[x][y+1]*p[x][y][2];
    if ( x < n && dp[x+1][y] < -0.5 ) dfs ( x+1 , y );
    dp[x][y] += dp[x+1][y]*p[x][y][3];
    if ( fabs ( 1.0 - p[x][y][1] ) < eps ) dp[x][y] = 0.0;
    else dp[x][y] /= ( 1.0 - p[x][y][1] );
}
int main ( )
{
    while ( ~scanf ( "%d%d" , &n , &m ) )
    {
        for ( int i = 1 ; i <= n ; i++ )
            for ( int j = 1 ; j <= m ; j++ )
                for ( int t = 1 ; t < 4 ; t++ )
                    scanf ( "%lf" , &p[i][j][t] );
        for ( int i = 0 ; i <= n ; i++ )
            for ( int j = 0 ; j <= m ; j++ )
                dp[i][j] = -1.0;
        dfs ( 1 , 1 );
        printf ( "%.3lf\n" , dp[1][1] );
    }
}