Codeforces 148D
设$dp(i, j)$为轮到王妃选鼠的时候白鼠有$i$只,黑鼠有$j$只时赢的概率。
分4种情况.
1、王妃选到白,则概率为$\frac{i}{i+j}$
2、龙选到白,王妃输了,则概率为$0$
3、王妃选到黑,龙选到黑,逃了黑,转移到$dp(i, j-3)$,则概率为$\frac{j}{i+j} \times \frac{j-1}{i+j-1} \times \frac{j-2}{i+j-2}$
4、王妃选到黑,龙选到黑,逃了白,转移到$dp(i-1, j-2)$,则概率为$\frac{j}{i+j} \times \frac{j-1}{i+j-1} \times \frac{i}{i+j-2}$
那么综上所述可以得到状态转移方程
$dp(i,j) = \frac{i}{i+j} + \frac{j}{i+j} \times \frac{j-1}{i+j-1} \times \frac{j-2}{i+j-2} \times dp(i, j-3)+\frac{j}{i+j} \times \frac{j-1}{i+j-1} \times \frac{i}{i+j-2} \times dp(i-1, j-2)$
注意不要一起乘,不然溢出。
Codeforces Submission
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<cmath>
#define ms(i, j) memset(i, j, sizeof i)
#define LL long long
#define db double
using namespace std;
const int MAXN = 1000 + 5;
int w, b;
db dp[MAXN][MAXN];
void clean() {
ms(dp, 0);
}
void solve() {
clean();
for (int i=1;i<=w;i++) dp[i][0] = 1.0;
for (int i=1;i<=w;i++) {
for (int j=1;j<=b;j++) {
dp[i][j] += (db)i / (db)(i + j);
db p1, p2;
if (j-3>=0) p1 = (db)j / (db)(i+j) * (db)(j-1) / (db)(i+j-1) * (db)(j-2) / (db)(i+j-2), dp[i][j] += dp[i][j-3] * p1;
if (j-2>=0) p2 = (db)j / (db)(i+j) * (db)(j-1) / (db)(i+j-1) * (db)i / (db)(i+j-2), dp[i][j] += dp[i-1][j-2] * p2;
}
}
printf("%.9f\n", dp[w][b]);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("1.in", "r", stdin);freopen("1.out", "w", stdout);
#endif
while(scanf("%d%d", &w, &b)==2) solve();
return 0;
}
记忆化搜索写法:
Codeforces Submission
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ms(i, j) memset(i, j, sizeof i)
#define LL long long
#define db double
#define FN2 "cf148d"
using namespace std;
const int MAXN = 1000 + 5;
db dp[MAXN][MAXN];
int w, b;
db dfs(int i, int j) {
if (i <= 0) return 0;
if (j <= 0) return 1;
if (dp[i][j]>=0) return dp[i][j];
dp[i][j] = 0;
dp[i][j] += (db)i / (db)(i + j);
if (j - 3 >= 0) {
dp[i][j] += ((db)j / (db)(i + j) * (db)(j - 1) / (db)(i + j - 1) * (db)(j - 2) / (db)(i + j - 2)) * dfs(i, j - 3);
}
if (i - 1 >= 0 && j - 2 >= 0) {
dp[i][j] += ((db)j / (db)(i + j) * (db)(j - 1) / (db)(i + j - 1) * (db)(i) / (db)(i + j - 2)) * dfs(i - 1, j - 2);
}
return dp[i][j];
}
void clean() {
ms(dp, -1);
}
void solve() {
clean();
printf("%.9f\n", dfs(w, b));
}
int main() {
#ifndef ONLINE_JUDGE
freopen(FN2".in", "r", stdin); freopen(FN2".out", "w", stdout);
#endif
scanf("%d%d", &w, &b), solve();
fclose(stdin); fclose(stdout);
return 0;
}