LightOJ 1030 - Discovering Gold - [概率DP]
题目链接: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]的更多相关文章
- LightOJ 1030 Discovering Gold (概率/期望DP)
题目链接:LightOJ - 1030 Description You are in a cave, a long cave! The cave can be represented by a \(1 ...
- LightOj 1030 - Discovering Gold(dp+数学期望)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1030 题意:在一个1*n 的格子里,每个格子都有相应的金币数,走到相应格子的话,就会得 ...
- Light OJ 1030 - Discovering Gold(概率dp)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1030 题目大意:有一个很长的洞穴, 可以看做是1-n的格子.你的起始位置在1的 ...
- LightOJ - 1030 Discovering Gold —— 期望
题目链接:https://vjudge.net/problem/LightOJ-1030 1030 - Discovering Gold PDF (English) Statistics For ...
- LightOJ 1030 Discovering Gold (期望)
https://vjudge.net/problem/LightOJ-1030 题意: 在一个1×N的格子里,每个格子都有相应的金币数,走到相应格子的话,就会得到该格子的金币. 现在从1格子开始,每次 ...
- LightOJ 1030 Discovering Gold(概率DP)题解
题意:1~n每格都有金子,每次掷骰子,掷到多少走几步,拿走那格的金子,问你金子的期望 思路:dp[i]表示从i走到n金子的期望,因为每次最多走1<=x<=6步,所以dp[i] = a[i] ...
- LightOJ 1030 Discovering Gold(期望 概率)
正推,到达i的概率为p[i],要注意除了1和n外,到达i的概率并不一定为1 概率表达式为p[i] += p[j] / min(n - j, 6) 从j带过来的期望为exp[i] += exp[j] / ...
- 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 ...
- 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 ...
随机推荐
- Eclipse------使用Maven install出错:编码GBK的不可映射字符
使用Maven install时报错:编码GBK的不可映射字符 原因:Maven默认使用GBK进行编码 解决方法: 在pom.xml文件中添加如下代码即可 <project> <pr ...
- Java实现两个变量的互换(不借助第3个变量)
创建一个类,在该类中定义两个变量并为其指定初始值,然后交换两个变量的值,要求不允许借助第三个变量,只能使用异或运行实现两个变量值的交换. import java.util.Scanner; publi ...
- linux_开发软件安装=命令步骤
1.Linux 操作系统软件安装以及redis 学习 JDK ----- Java开发运行环境 Tomcat -- WEB程序的服务器 MySQL --- 持久化存储数据 Re ...
- Spring-----配置及对象初始化(1)
一,配置文件进行Spring初始化 1,配置文件编写 <?xml version="1.0" encoding="utf-8" ?> <con ...
- 使用HttpClient访问url的工具类
maven依赖jar包配置: <dependency> <groupId>org.apache.httpcomponents</groupId> <artif ...
- yii 前端js动态添加验证规则
在使用 activeForm 生成表单及验证时,默认是按照 model 里的 rules 生成js验证,model 验证在加载完页面后生效,不可修改,如果需要扩展.动态验证,需要使用js来配合 直接上 ...
- 【代码审计】LaySNS_v2.2.0 前台XSS跨站脚本漏洞
0x00 环境准备 LaySNS官网:http://www.laysns.com/ 网站源码版本:LaySNS_v2.2.0 程序源码下载:https://pan.lanzou.com/i0l38 ...
- linux系统cpu和内存占用率
1.top 使用权限:所有使用者 使用方式:top [-] [d delay] [q] [c] [S] [s] [i] [n] [b] 说明:即时显示process的动态 d :改变显示的更新速度,或 ...
- iOS AOP编程思想及实践
什么是 AOP Wikipedia 上的 AOP 定义: In computing, aspect-oriented programming (AOP) is a programming paradi ...
- 《转》Python学习(19)-python函数(二)-关于lambda
转自http://www.cnblogs.com/BeginMan/p/3178103.html 一.lambda函数 1.lambda函数基础: lambda函数也叫匿名函数,即,函数没有具体的名称 ...