CodeForces 148D Bag of mice probability dp

Reference link: https://www.cnblogs.com/Howe-Young/p/5514371.html

 

Title: Give you w white mice and b black mice, put them in a bag, princess fetch first, dragon later, when princess fetch any one from the rest, when dragon takes Either one will be taken the rest of the time, but one will be randomly popped out after it is taken. The probability of getting each mouse is the same, and the jump is the same. First get the white mouse to win, and ask the probability that princess will win in the end.

Idea: Probability dp, if the princess can be divided into two situations, then this question is recursive, I wrote it using memory search. First, use dp [i] [j] to represent the probability that there are i white and j black princess in the bag to win. Then there are two cases:
1. This step can win, then the white is directly obtained, the probability is i / (i + j);
2. This step does not win, then the current must be black, because the last To make princess win, so, then dragon won't win, and now there is a problem, the color of the jumping mouse, then it is divided into two cases:
  1). The jumping out is white. The probability is j / (i + j) * (j-1) / (i + j-1) * (i) / (i + j-2) * dp [i-1] (j-2]
  2). The bit that jumped out was black. The probability is that j / (i + j) * (j-1) / (i + j-1) * (j-2) / (i + j-2) * dp [i] [j-3] is
pushed here Basically came out, the remaining boundary conditions. If i == 0, then the probability must be 0, if i> 0 && j == 0 then the probability must be 1.

 

 

Code:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<queue>
 6 #include<map>
 7 #include<vector>
 8 #include<math.h>
 9 #define mem(a,x) memset(a,x,sizeof(a))
10 using namespace std;
11 typedef long long LL;
12 const int maxn=1005;
13 const int mod=26;
14 const  int INF = 0x3f3f3f3f ;
 15  const  int Times = 10 ;
 16  const  int N = 5500 ;
 17  double dp [maxn] [maxn];
 18  double dfs ( int w, int b)
 19  {
 20      // b <0 The behavior of a dfs is not desirable 
21      if (w <= 0 || b < 0 ) return  0 ;
 22      if (w> 0 && b == 0 ) returndp [w] [b] = 1 ;
 23      if (dp [w] [b]! = -1 ) return dp [w] [b];
 24      double temp1 = ( double ) w / (w + b);   / / Get the white ball directly 
25      double temp2 = ( double ) b / (w + b);
 26      dp [w] [b] = temp1;
 27      if (w + b> 2 ) // To prevent the denominator from being 0 
28      {
 29          double ans1 = dfs (w, b- 3 ) * (b- 1 ) / (w + b- 1 ) * (b- 2 ) / (w + b- 2 );
30         double ans2=dfs(w-1,b-2)*(b-1)/(w+b-1)*(w)/(w+b-2);
31         temp2*=(ans1+ans2);
32         dp[w][b]+=temp2;
33     }
34     return dp[w][b];
35 }
36 int main()
37 {
38     int w,b;
39     cin>>w>>b;
40     for(int i=0;i<=w;++i)
41     {
42         for(int j=0;j<=b;++j)
43             dp[i][j]=-1;
44     }
45     printf("%.9lf\n",dfs(w,b));
46 }

 

Guess you like

Origin www.cnblogs.com/kongbursi-2292702937/p/12672694.html