CoderForce 148D-Bag of mice (概率DP求概率)

题目大意:美女与野兽在玩画鸽子的游戏。鸽子在用黑布遮住的笼子里,白色的有w只,黑色的有b只,每次拿出一只作画,谁先画到白色的鸽子谁就赢。美女首先画,因为野兽太丑,它每次画的时候都会吓跑一只鸽子,所有出笼子的鸽子都不在进去。求美女赢得概率。(设定假如没有人画到白色鸽子,算野兽赢)。

题目分析:这道题不难,很显然的概率DP。这是我第一次写概率DP,纪念一下。。。

 

代码如下:

# include<iostream>
# include<cstdio>
# include<vector>
# include<list>
# include<queue>
# include<cstring>
# include<set>
# include<map>
# include<string>
# include<cmath>
# include<algorithm>
using namespace std;

double dp[1005][1005];

double DP(int w,int b)
{
    if(dp[w][b]!=-1.0) return dp[w][b];
    if(w<=0) return dp[w][b]=0.0;
    if(b<=0) return dp[w][b]=1.0;
    double res1=(double)w/(double)(w+b);
    double res2=((double)b/(double)(w+b))*((double)(b-1)/(double)(w+b-1));
    double t=0.0;
    if(b-2>0)
        t+=((double)(b-2)/(double)(w+b-2))*DP(w,b-3);
    if(w-1>0)
        t+=((double)(w)/(double)(w+b-2))*DP(w-1,b-2);
    res2*=t;
    return dp[w][b]=res1+res2;
}

int main()
{
    int w,b;
    while(~scanf("%d%d",&w,&b))
    {
        for(int i=0;i<=w;++i)
            for(int j=0;j<=b;++j)
                dp[i][j]=-1.0;
        printf("%.9lf\n",DP(w,b));
    }
    return 0;
}

  

你可能感兴趣的:(CoderForce 148D-Bag of mice (概率DP求概率))