• 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
    ",DP(w,b));
        }
        return 0;
    }
    

      

  • 相关阅读:
    ABAP术语-Logical Lock
    ABAP术语-Lock Object
    ABAP术语-Lock Mode
    ABAP术语-Lock Argument
    ABAP术语-Key Field
    Python面向对象基础-day6
    Python中的内置模块与生成器迭代器-day5
    Python高阶函数与函数装饰器-day4
    Python字符编码与函数基本使用-day3
    Nginx内置变量及正则语法
  • 原文地址:https://www.cnblogs.com/20143605--pcx/p/5180592.html
Copyright © 2020-2023  润新知