搜索dp~注意 w 和 b 的值要大于0 ~否则会越界~
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cstdlib>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=1024;
const double eps = 1e-13;
double dp[maxn][maxn];
bool vis[maxn][maxn];
double d2[maxn][maxn];
bool v2[maxn][maxn];
int ww,bb;
double dwin(int w,int b);
double pwin (int w,int b)
{
if(vis[w][b])
{
return dp[w][b];
}
double wx=w;
double bx=b;
double temp = 0.0;
temp += wx / (wx + bx);
temp += ( bx / ( wx + bx ) ) * ( 1.0 - dwin( w , b - 1 ) );
vis[w][b] = true;
dp[w][b] = temp;
return dp[w][b];
}
double dwin (int w,int b)
{
if(v2[w][b])
{
return d2[w][b];
}
double temp = 0.0;
double tw=0.0;
double tb=0.0;
double wx=w;
double bx=b;
temp += wx / (wx+bx);
if(w-1>=0 && b-1>=0)
{
tw = ( wx / ( wx + bx - 1.0 ) ) * ( 1.0 - pwin ( w - 1 , b - 1) );
}
if(b-2>=0)
{
tb = ( ( bx - 1.0 ) / ( wx + bx - 1.0 ) ) * ( 1.0 - pwin ( w , b - 2 ) );
}
temp += ( bx / ( wx + bx ) ) * (tw + tb);
v2[w][b] = true;
d2[w][b] = temp;
return d2[w][b];
}
int main()
{
memset(vis,false,sizeof(vis));
memset(v2,false,sizeof(v2));
for(int i=1;i<maxn;i++)
{
vis[i][0] = true;
dp[i][0] = 1.0;
vis[0][i] = true;
dp[0][i] = 0.0;
v2[i][0] = true;
d2[i][0] = 1.0;
v2[0][i] = true;
d2[0][i] = 1.0;
}
v2[0][0]=vis[0][0]=true;
dp[0][0]=0.0;
d2[0][0]=1.0;
cout.setf(ios::fixed);
while(cin>>ww>>bb)
{
cout<<setprecision(9)<<pwin(ww,bb)<<endl;
}
return 0;
}