Codeforces Round #105 (Div. 2) (D. Bag of mice(概率DP))

题目链接:点击打开链接

题意:

  1. 原来袋子里有w只白鼠和b只黑鼠 
  2. 龙和王妃轮流从袋子里抓老鼠。谁先抓到白色老师谁就赢。 
  3. 王妃每次抓一只老鼠,龙每次抓完一只老鼠之后会有一只老鼠跑出来。 
  4. 每次抓老鼠和跑出来的老鼠都是随机的。 
  5. 如果两个人都没有抓到白色老鼠则龙赢。王妃先抓。 
  6. 问王妃赢的概率。 
思路:概率DP, 用d[a][b][id]表示当前还剩a只白鼠, b只黑鼠, 轮到i == 0 ? 王妃 : 龙 抓老鼠了, 最终王妃赢的概率。 然后转移即可。

细节参见代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
typedef long double ld;
const ld eps = 1e-9, PI = 3.1415926535897932384626433832795;
const int mod = 1000000000 + 7;
const int INF = 0x3f3f3f3f;
// & 0x7FFFFFFF
const int seed = 131;
const ll INF64 = ll(1e18);
const int maxn = 1000 + 10;
int T,n,m,vis[maxn][maxn][2],kase = 0, w, b;
double d[maxn][maxn][2];
double dp(int a, int b, int id) {
    double& ans = d[a][b][id];
    if(a == 0) return 0;
    if(a != 0 && b == 0) {
        if(id) return 0;
        else return 1;
    }
    if(vis[a][b][id] == kase) return ans;
    vis[a][b][id] = kase;
    ans = 0;
    if(id) {
        if(b > 1) ans += dp(a, b-2, id^1) * (double)b/(double)(a + b) * (double)(b-1)/(double)(a+b-1);
        if(b > 0 && a > 0) ans += dp(a-1, b-1, id^1) * (double)b/(double)(a + b) * (double)a / (double)(a + b - 1);
    }
    else {
        if(a > 0) ans += (double)(a)/(a + b);
        if(b > 0) ans += dp(a, b-1, id^1) * (double)b / (double)(a + b);
    }
    return ans;
}
int main() {
    scanf("%d%d",&w,&b);
    ++kase;
    double ans = dp(w, b, 0);
    printf("%.10f\n",ans);
    return 0;
}


你可能感兴趣的:(codeforces,ACM-ICPC,概率DP)