ZOJ3822 ACM-ICPC 2014 Asian semi finals Mudanjiang Division Field Competition D Question Domination probability DP (two solutions)
Question address: Click to open the link
There are two ways to solve this question. The first is to directly look for expectations. Similar to poj 2096, the difference is that this step is limited. So the number of iterations is required.
# Include
# Include
# Include
# Define maxn 55 // if it is written as 50 + 10 at the beginning, maxn * maxn will be much smaller than the using namespace std once; double dp [maxn] [maxn] [maxn * maxn]; int N, M, T; int main () {while (~ Scanf ("% d", & T) while (T --) {scanf ("% d", & N, & M); memset (dp, 0, sizeof (dp); for (int I = N; I> = 0; I --) for (int j = M; j> = 0; j --) {if (I = N & j = M) continue; // state definition: dp [I] [j] [k] covers the j column of row I in step k. In this case, the expected for (int k = I * j; k> = max (I, j); k --) // The number of steps cannot be greater than I * j, the maximum value in j has already covered so many {double p0 = 1.0 * (I * j-k)/(N * M-k); double p1 = 1.0 * (M-j) * I/(N * M-k); double p2 = 1.0 * (N-I) * j/(N * M-k ); double p3 = 1.0 * (N-I) * (M-j)/(N * M-k ); dp [I] [j] [k] = dp [I] [j] [k + 1] * p0 + dp [I] [j + 1] [k + 1] * p1 + dp [I + 1] [j] [k + 1] * p2 + dp [I + 1] [j + 1] [k + 1] * p3 + 1 ;}} printf ("%. 12lf \ n ", dp [0] [0] [0]);} return 0 ;}
The second is to calculate the probability and calculate the expectation after the event is completed. (This method cannot be used for questions with no limit on the number of steps)
#include
#include
#include
#includeusing namespace std;double dp[55][55][55*55];const double eps=1e-8;int main(){ // freopen("in.txt","r",stdin); int t; cin>>t; while(t--) { int n,m; scanf("%d%d",&n,&m); memset(dp,0,sizeof(dp)); memset(a,0,sizeof(a)); dp[1][1][1]=1; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(i==n && j==m) break; for(int k=max(i,j);k<=i*j;k++) { dp[i][j][k+1]+=dp[i][j][k]*(i*j-k)/(n*m-k); dp[i+1][j][k+1]+=dp[i][j][k]*(n-i)*j/(n*m-k); dp[i][j+1][k+1]+=dp[i][j][k]*(m-j)*i/(n*m-k); dp[i+1][j+1][k+1]+=dp[i][j][k]*(n-i)*(m-j)/(n*m-k); } } } double sum=0; for(int i=max(n,m);i<=n*m;i++) sum+=dp[n][m][i]*i; printf("%.12f\n",sum); } return 0;}