Discovering Gold
You are in a cave, a long cave! The cave can be represented by a 1 x N grid. Each cell of the cave can contain any amount of gold.
Initially you are in position 1. Now each turn you throw a perfect 6 sided dice. If you get X in the dice after throwing, you add X to your position and collect all the gold from the new position. If your new position is outside the cave, then you keep throwing again until you get a suitable result. When you reach the Nth position you stop your journey. Now you are given the information about the cave, you have to find out the expected number of gold you can collect using the given procedure.
Input
Input starts with an integer T (≤ 100), denoting the number of test cases.
Each case contains a blank line and an integer N (1 ≤ N ≤ 100) denoting the dimension of the cave. The next line contains N space separated integers. The ith integer of this line denotes the amount of gold you will get if you come to the ith cell. You may safely assume that all the given integers will be non-negative and no integer will be greater than 1000.
Output
For each case, print the case number and the expected number of gold you will collect. Errors less than 10-6 will be ignored.
Sample Input
3
1
101
2
10 3
3
3 6 9
Sample Output
Case 1: 101.0000000000
Case 2: 13.000
Case 3: 15
题目的大意就是,有n个格子排在一行,每一个格子里面有些钱,你的初始位置在1,每次你可以投掷一个6面骰子,然后将你的位置增加投掷到的数.如果越界的话重投,求期望获得多少钱.
设E[i]为从格子i走到终点的期望获得的钱.则E[n]=a[n],我们的目标是求E[1],所以要反着刷.
一个简单的期望DP.在不越界的情况下,E[i]可以被修改E[i+1],E[i+2],E[i+3],E[i+4],E[i+5],E[i+6]到
普遍的,假设第i个位置能被j1,j2,j3...jcnt这些位置修改,那么从i走到每一个位置的概率都是1/cnt,期望得到钱为E[j]/cnt,则E[i]+=E[j]/cnt;
然后就解完了.

1 #include<cstdio>
2 #include<cstring>
3 #include<algorithm>
4 using namespace std;
5 int n,a[105];
6 double E[105];
7 int main(){
8 int T; scanf("%d",&T);
9 for (int Ts=1; Ts<=T; Ts++){
10 scanf("%d",&n);
11 for (int i=1; i<=n; i++) scanf("%d",&a[i]);
12 memset(E,0,sizeof E);
13 E[n]=a[n];
14 for (int i=n-1; i>=1; i--){
15 E[i]=a[i];
16 int cnt=6; if (n-i<6) cnt=n-i;
17 for (int j=i+1; j<=min(n,i+6); j++){
18 E[i]+=E[j]/(double)cnt;
19 }
20 }
21 printf("Case %d: %.10lf\n",Ts,E[1]);
22 }
23 return 0;
24 }
本文探讨了一个有趣的概率问题,即在一个装满金币的长洞穴中,通过投掷六面骰子前进并收集金币,求解期望获得的金币总数。采用期望动态规划(DP)算法进行解答,详细解释了如何逆向计算每个位置的期望收益。

被折叠的 条评论
为什么被折叠?



