题目链接:https://cn.vjudge.net/problem/LightOJ-1030

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~n,每个格子里有价值Gi的宝藏;

现在,扔一个六面的标准的骰子,按得到的点数走格子,起点为1;

如果在某个格子,扔出骰子之后,得到的点数会让你走到编号大于n的格子,就不算数,重新扔,直到扔到一个你能走点数为止;

如果你走到了编号n的格子,就停止;

求得到宝藏价值的期望值。

题解:

每个格子的价值为Gi,我们只要求出每个格子可能被走到的概率Pi,那么我们求出Σ( Gi * Pi )即为答案;

显然是个概率DP题,动态转移求出走到每个格子的概率即可;

AC代码:

 #include<cstdio>
#include<cstring>
#define MAXN 105
#define min(a,b) (a<b)?a:b
int n,grid[MAXN];
double dp[MAXN];
int main()
{
int t;
scanf("%d",&t);
for(int kase=;kase<=t;kase++)
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",grid+i); memset(dp,,sizeof(dp));
dp[]=;
for(int i=;i<=n;i++)
{
int k=min(,n-i);
for(int j=;j<=k;j++) dp[i+j]+=dp[i]*(1.0/k);
} double ans=;
for(int i=;i<=n;i++) ans+=dp[i]*grid[i];
printf("Case %d: %.7lf\n",kase,ans);
}
}

PS. becky大佬有期望DP的做法:http://blog.csdn.net/becky_w/article/details/78247858

LightOJ 1030 - Discovering Gold - [概率DP]的更多相关文章

  1. LightOJ 1030 Discovering Gold (概率/期望DP)

    题目链接:LightOJ - 1030 Description You are in a cave, a long cave! The cave can be represented by a \(1 ...

  2. LightOj 1030 - Discovering Gold(dp+数学期望)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1030 题意:在一个1*n 的格子里,每个格子都有相应的金币数,走到相应格子的话,就会得 ...

  3. Light OJ 1030 - Discovering Gold(概率dp)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1030 题目大意:有一个很长的洞穴, 可以看做是1-n的格子.你的起始位置在1的 ...

  4. LightOJ - 1030 Discovering Gold —— 期望

    题目链接:https://vjudge.net/problem/LightOJ-1030 1030 - Discovering Gold    PDF (English) Statistics For ...

  5. LightOJ 1030 Discovering Gold (期望)

    https://vjudge.net/problem/LightOJ-1030 题意: 在一个1×N的格子里,每个格子都有相应的金币数,走到相应格子的话,就会得到该格子的金币. 现在从1格子开始,每次 ...

  6. LightOJ 1030 Discovering Gold(概率DP)题解

    题意:1~n每格都有金子,每次掷骰子,掷到多少走几步,拿走那格的金子,问你金子的期望 思路:dp[i]表示从i走到n金子的期望,因为每次最多走1<=x<=6步,所以dp[i] = a[i] ...

  7. LightOJ 1030 Discovering Gold(期望 概率)

    正推,到达i的概率为p[i],要注意除了1和n外,到达i的概率并不一定为1 概率表达式为p[i] += p[j] / min(n - j, 6) 从j带过来的期望为exp[i] += exp[j] / ...

  8. LightOJ 1030 Discovering Gold(期望)

    Description You are in a cave, a long cave! The cave can be represented by a 1 x N grid. Each cell o ...

  9. LightOJ 1030 Discovering Gold

    期望,$dp$. 设$ans[i]$为$i$为起点,到终点$n$获得的期望金币值.$ans[i]=(ans[i+1]+ans[i+2]+ans[i+3]+ans[i+4]+ans[i+5]+ans[i ...

随机推荐

  1. 从Python学习中得到的一点启发 - Java逆向索引ArrayList

    看了几天Python,感觉记忆力不行了,很多东西记不住了.但是终归是得到了一点知识:重写一个ArrayList,允许从负值的索引得到指定的项.然后写一个得到斐波拉契数组的方法,这种方法要比递归调用的方 ...

  2. iOS开发之-- 字符串的操作,去掉某一个字符或者替换成其他字符

    一个简单的操作,记录下: NSString *strUrl = [urlString stringByReplacingOccurrencesOfString:@" " withS ...

  3. ios开发之 -- NSString指定字体高亮显示

    一个简单的小需求,就是在一个字符串里面,指定一部分字节高亮显示,代码如下: NSString *descStr = @"需要高亮显示的字符"; NSString *nickStr ...

  4. 【Cesium】物体显示

    viewer.zoomTo(entity1); viewer.zoomTo(viewer.entities); viewer.camera.flyTo({ destination: Cesium.Ca ...

  5. str.split()与re.split()的区别

    str.split(): >>>'hello, world'.split() >>>['hello,','world'] >>>'hello, w ...

  6. Maven构建Web项目

    1. Maven的作用: [清理项目]→[编译项目]→[测试项目]→[生成测试报告]→[打包项目]→[部署项目] 2. 安装maven maven对JDK版本有要求,所以要设置好JDK的环境变量. M ...

  7. [Git] 解决 insufficient permission for adding an object to repository database

    [环境] OS: CentOS 6.5 Git: 1.7.1 [症状描述] Git 中心仓库路径 ~/project.git,克隆库路径 ~/project.clone,克隆库中包含一个文件 ~/pr ...

  8. springboot 集成elasticsearch

    In this article, we will discuss about “How to create a Spring Boot + Spring Data + Elasticsearch Ex ...

  9. JS基础---->javascript的基础(二)

    记载javascript的一些基础的知识.我们在春风秋雨中无话不说,又在春去秋来中失去了联系. js中string类型 一.字符方法:charAt() 和 charCodeAt() var strin ...

  10. SVN —— 如何设置代理

    如果在使用SVN下载外网的资源时,出现这样的提示:No such host is known. 或者 不知道这样的主机,可能是机器网络的问题. 如果浏览器能够正常访问外网,那应该是网络设置了代理的问题 ...