f ( i , j ) 表示到(i,j)这个位置的期望
f ( i , j ) =( f (i+1,j)p1 + f(i,j+1) p2 + 2 ) / p3
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1010 ;
const int Inf = 1e9;
double p0 [N] [N] , p1 [N] [N] , p2 [N] [N] , f [N] [N] ;
int n , m ;
int main () {while (~scanf ( "%d%d" , & n , & m )) {memset ( f , 0 , sizeof f ) ;for ( int i = 1 ; i <= n ; ++ i ) for ( int j = 1 ; j <= m ; ++ j ) {scanf ( "%lf" , & p0 [i] [j] ) ;scanf ( "%lf" , & p1 [i] [j] ) ;scanf ( "%lf" , & p2 [i] [j] ) ;}for ( int i = n ; i >= 1 ; -- i ) for ( int j = m ; j >= 1 ; -- j ) {if ( i == n && j == m ) continue ;if ( p0 [i] [j] == 1.0 ) continue ;f [i] [j] = ( f [i+1] [j] * p2 [i] [j] + f [i] [j+1] * p1 [i] [j] + 2.0 ) / ( 1 - p0 [i] [j] ) ;}printf ( "%.3lf\n" , f [1] [1] ) ;}return 0 ;
}