Title: http://poj.org/problem?id=2096
It is obvious that the state $f (I,J) $ indicates the set of I bugs, the desired number of steps from the set of J system distances.
Then there
$f (I,j), f (i,j) \ P0 = i/n \cdot j/m$
$f (I+1,j), f (i,j) \ P1 = (1-i/n) \cdot j/m$
$f (I+1,j), f (i,j) \ P2 = i/n \cdot (1-j/m) $
$f (I+1,j+1), f (i,j) \ P3 = (1-i/n) \cdot (1-j/m) $
Can be found to be an infinite recursive shape such as:
$f (i,j) = i/n \cdot j/s \cdot (f (i,j) + 1) + S $
$f (i,j) = (ij)/(NM-IJ) + (NS)/(NS-IJ) \cdot S $
Here $s$ represents $p1\cdot F (i+1,j) $ $P 2 \cdot F (i,j+1) $ $P 3 \cdot F (i+1,j+1) $ et cetera
And then you can do it.
#include <cstdio>#include<cstring>#include<algorithm>#defineN 1010#defineLD Doubleusing namespacestd;intn,m; LD F[n][n]; LD S (intIintj) {LD ans=0; if(i<n) ans + = (1.0-i/(LD) n) * j/(LD) m * (1.0+ f[i+1][j]); if(j<m) ans + = i/(LD) n * (1.0-j/(LD) m) * (1.0+ f[i][j+1]); if(i<n&&j<m) ans + = (1.0-i/(LD) n) * (1.0-j/(LD) m) * (1.0+ f[i+1][j+1]); returnans;}intMain () {scanf ("%d%d",&n,&m); F[N][M]=0; for(inti=n;~i;i--) for(intj=m;~j;j--){ if(i==n&&j==m)Continue; F[I][J]= i*j/(LD) (N*M-I*J) + (n*m)/(LD) (N*M-I*J) *S (I,J); } printf ("%.4lf\n", f[0][0]); return 0;}
View Code
POJ 2096 Collecting Bugs