当前位置: 代码迷 >> 综合 >> hdu4405[Aeroplane chess] 有点懂了期望概率DP
  详细解决方案

hdu4405[Aeroplane chess] 有点懂了期望概率DP

热度:55   发布时间:2023-11-06 07:29:45.0

f ( i)表示走到i的期望
f ( i ) = f ( i+j ) * ( 1 / 6 )
f ( i ) = nxt [i]

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1e5 + 10 ;int nxt [N] ;
double f [N] ;int main () {int n , m ;while ( scanf ( "%d%d" , & n , & m ) != EOF && ( n != 0 || m != 0 ) ) {memset ( f , 0 , sizeof f ) ;memset ( nxt , 0 , sizeof nxt ) ;for ( int i = 1 ; i <= m ; ++ i ) {int x , y ;scanf ( "%d%d" , & x , & y ) ;nxt [x] = y ;}for ( int i = n-1 ; i >= 0 ; -- i ){if ( nxt [i] ) { f [i] = f [ nxt [i] ] ; continue ; }for ( int j = 6 ; j >= 1 ; -- j )f [i] += ( f [i+j] ) * ( 1.0 / 6 )  ;f [i] += 1 ;}printf ( "%.4lf\n" , f [0] ) ;}return 0 ;
}