[LightOJ 1030] Discovering Gold

本文探讨了一个有趣的概率问题,即在一个装满金币的长洞穴中,通过投掷六面骰子前进并收集金币,求解期望获得的金币总数。采用期望动态规划(DP)算法进行解答,详细解释了如何逆向计算每个位置的期望收益。

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 }
View Code

 

转载于:https://www.cnblogs.com/whc200305/p/7204528.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值