ZOJ 3329 One Person Game——期望dp
原创
©著作权归作者所有:来自51CTO博客作者软糖酱八号机的原创作品,请联系作者获取转载授权,否则将追究法律责任
题意:
有三个骰子,分别有k1,k2,k3个面,每次掷骰子,如果三个面分别为a,b,c则分数置0,否则加上三个骰子的分数之和,当分数大于n时结束。求游戏的期望步数。初始分数为0
思路:
设dp[i]表示达到i分时到达目标状态的期望,pk为投掷k分的概率,p0为回到0的概率
则dp[i]=∑(pk*dp[i+k])+dp[0]*p0+1;
都和dp[0]有关系,而且dp[0]就是我们所求,为常数
设dp[i]=A[i]*dp[0]+B[i];
代入上述方程右边得到:
dp[i]=∑(pk*A[i+k]*dp[0]+pk*B[i+k])+dp[0]*p0+1
=(∑(pk*A[i+k])+p0)dp[0]+∑(pk*B[i+k])+1;
明显A[i]=(∑(pk*A[i+k])+p0)
B[i]=∑(pk*B[i+k])+1
先递推求得A[0]和B[0].
那么 dp[0]=B[0]/(1-A[0]);
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1000;
double p[maxn], A[maxn], B[maxn];
int main() {
int T, n, k1, k2, k3, a, b, c;
scanf("%d", &T);
for (int kase = 1; kase <= T; kase++) {
scanf("%d%d%d%d%d%d%d", &n, &k1, &k2, &k3, &a, &b, &c);
double p0 = 1.0/k1/k2/k3;
memset(p, 0, sizeof(p));
for (int i = 1; i <= k1; i++) {
for (int j = 1; j <= k2; j++) {
for (int k = 1; k <= k3; k++) {
if (i != a || j != b || k != c) p[i+j+k] += p0;
}
}
}
memset(A, 0, sizeof(A));
memset(B, 0, sizeof(B));
for (int i = n; i >= 0; i--) {
A[i] = p0, B[i] = 1;
for (int j = 1; j <= k1+k2+k3; j++) {
A[i] += A[i+j]*p[j];
B[i] += B[i+j]*p[j];
}
}
printf("%.16f\n", B[0] / (1.0 - A[0]));
}
return 0;
}