DP [x] [Y]: Now there are X white mice, y black mice, and the probability that the princess will win.
So:
Assume that the princess gets the white mouse directly. The probability is X/(x + y), and the princess wins.
Assuming that the princess gets the black mouse, the probability is Y/(x + y), then the princess assumes that to win, the dragon must get the black mouse, the probability is (Y-1)/(x + Y-1 );
Then the probability of the escaped mouse being a black mouse is (y-2)/(x + y-2), the probability of a white mouse being (X)/(x + y-2 );
So DP [x] [Y] = x/(x + y) + Y/(x + y) * (Y-1)/(x + Y-1) * (y-2) /(x + y-2) * DP [x] [Y-3] + (X)/(x + y-2) * DP [x-1] [y-2]);
You can also perform deep-search with memory, and perform recursive DP directly.
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<stdlib.h>#include<vector>#include<cmath>#include<queue>#include<set>#include<stack>using namespace std;#define N 100010#define LL long long#define INF 0xfffffff#define maxn 1100double dp[maxn][maxn];double dos(int x,int y){ if(x<0||y<0)return 0; if(x==0)return 0; if(y==0)return 1; if(dp[x][y]>-0.5)return dp[x][y]; double a,b; a=1.0*x; b=1.0*y; dp[x][y]=a/(a+b); if(x+y>=3) { dp[x][y]+=(b*(b-1)/((a+b)*(a+b-1)))* (dos(x-1,y-2)*a/(a+b-2)+dos(x,y-3)*(b-2)/(a+b-2)); } // cout<<x<<" "<<y<<" "<<dp[x][y]<<endl; return dp[x][y];}int main(){ int a,b; while(~scanf("%d%d",&a,&b)) { memset(dp,-1,sizeof(dp)); printf("%.10lf\n",dos(a,b)); } return 0;}