codeforces 148D 【概率dp】

题目链接: codeforces 148D Bag of mice

题意:一个包里面有w只白老鼠和b只黑老鼠,公主与龙依次从包中拿老鼠,每次取一只,当龙拿时还会从包中溜走一只,先拿到老鼠的获胜,当背包中没老鼠时且之前没人拿到
白老鼠则龙获胜,问公主获胜的概率是多少。

题解:

设dp[i][j]为背包中有i只白老鼠j只黑老鼠时公主获胜的概率

则公主获胜的情况分成三种:

1.直接拿到白老鼠 p1=i/(i+j)

2.公主拿到黑老鼠,龙拿到黑老鼠,逃跑一只黑老鼠 p2=(j/(i+j)) ((j-1)/(i+j-1)) ((j-2)/(i+j-2)) *
dp[i][j-3]

3.公主拿到黑老鼠,龙拿到黑老鼠,逃跑一只黑老鼠 p3=(j/(i+j)) ((j-1)/(i+j-1)) (i/(i+j-2)) *
dp[i-1][j-2]

所以dp[i][j] = p1+p 大专栏  codeforces 148D 【概率dp】2+p3

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cmath>
#include<stdlib.h>
#include<string.h>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<sstream>

using namespace std;
#define MAX_N 100005
#define inf 0x3f3f3f3f
#define LL long  long
#define ull unsigned long long
const LL INF = 1e18;
const double eps = 1e-8;
//const int mod = 1e9+7;
//const double pi = acos(-1);
typedef pair<int, int>P;

double dp[1005][1005];
int main()
{
    int w, b;
    cin >> w >> b;
    memset(dp, 0, sizeof(dp));
    for(int i=1; i<=w; i++) {
        for(int j=0; j<=b; j++) {
            dp[i][j] = 1.0*i/(i+j);
            if(j-3 >= 0)
                dp[i][j] += (1.0*j/(i+j))*(1.0*(j-1)/(i+j-1))*(1.0*(j-2)/(i+j-2))*dp[i][j-3];
            if(j-2 >= 0)
                dp[i][j] += (1.0*j/(i+j))*(1.0*(j-1)/(i+j-1))*(1.0*i/(i+j-2))*dp[i-1][j-2];
        }
    }
    printf("%.9lfn", dp[w][b]);
}

猜你喜欢

转载自www.cnblogs.com/sanxiandoupi/p/11724260.html