Discovering Gold LightOJ - 1030 (概率dp)
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
题意:有一个1*n的洞穴,开始的时候你在第一个格子,你扔一个六面的筛子,扔到n你就走到你当前位置前面的第n个格子,然后你能得到这个格子里面存的黄金数(每到一个新格子就扔一次筛子)。
每个格子的黄金数已经给出,问你走到最后一个格子所能获得的总黄金数的期望。
思路:筛子只有1-6这6个数字,所以你每一步只能走到当前格子的前面6个格子中的一个,假设走到当前格子所能获得的期望值为dp[i],则dp[i] = ( dp[i+1]/6 + ... + dp[i+6]/6 ) + a[i]。(a[i]为当前格子含有的金子数)
想一想,你离终点越远,理论上你能获得的金子也就越多,因为可以走更多步,所以越接近起点,dp值越大。初始的dp【i】的值为第i个格子的金子数,然后要加上你走到的下一个位置所能
获得的金子的期望,下一个位置有六种可能,每种都是1/6;当然,还有特殊情况,那就是你当前位置与终点之间的距离小于6,那就特殊考虑,有几个格子上面的递推公式的分母就为几。
从终点往前递推,最后dp【1】的值就是答案。
看下面的代码理解一下吧(虽然我也是似懂非懂YwY)。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
int main()
{
int t,n,cnt=;
double a[],dp[];
cin>>t;
while(t--)
{
cin>>n;
memset(dp,,sizeof(dp));
for(int i=; i<=n; i++)
cin>>a[i];
dp[n] += a[n];
for(int i=n-;i>=;i--)
{
dp[i] += a[i];//当前点的期望等于后6个点的期望的 1/6 的和;
int len = min(n-i,);//判断当前点之后是否还有6个点 ,不够六个点则有几个算几个。
for(int j=i+; j<=i+ && j<=n; j++)
{
dp[i] += dp[j]/len;
}
}
cout<<"Case "<<++cnt<<": ";
printf("%.7lf\n",dp[]);
}
return ;
}
Discovering Gold LightOJ - 1030 (概率dp)的更多相关文章
- Discovering Gold LightOJ - 1030 || 概率与期望求法区别
		
#include<cstdio>//wrong_codes #include<algorithm> using namespace std; ],anss; ],T,TT,n, ...
 - lightoj 1030 概率dp
		
题目链接:http://lightoj.com/volume_showproblem.php?problem=1030 #include<cstdio> #include<cstri ...
 - Discovering Gold  lightoj 1030
		
一排1到n的格子,每个格子上有黄金 ai ,你最开始在 1 号,每一次投骰子决定到哪一个格子,超出1~n范围则重新投掷,你到了哪个格子就得到哪个格子的金币,问最终在n 能得到金币的期望. 思路:做题经 ...
 - A Dangerous Maze (II) LightOJ - 1395(概率dp)
		
A Dangerous Maze (II) LightOJ - 1395(概率dp) 这题是Light Oj 1027的加强版,1027那道是无记忆的. 题意: 有n扇门,每次你可以选择其中一扇.xi ...
 - Where to Run LightOJ - 1287(概率dp)
		
Where to Run LightOJ - 1287(概率dp) 题面长长的,看了半天也没看懂题意 不清楚的地方,如何判断一个点是否是EJ 按照我的理解 在一个EJ点处,要么原地停留五分钟接着走,要 ...
 - Light oj 1030 概率DP
		
D - Discovering Gold Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:32768 ...
 - LightOJ - 1151概率dp+高斯消元
		
概率dp+高斯消元 https://vjudge.net/problem/LightOJ-1151 题意:刚开始在1,要走到100,每次走的距离1-6,超过100重来,有一些点可能有传送点,可以传送到 ...
 - LightOJ 1038 概率dp
		
题意:给一个数n,每次除它的一个因子(等概率),问除到1的次数的期望是多少 题解:概率dp,对于一个数x,y是x的因子个数,因子是a1到ay,E(x)=(E(a1)+1)/y+...+(E(ay)+1 ...
 - loj 1030概率dp
		
题目链接:http://lightoj.com/volume_showproblem.php?problem=1030 思路:一直以来对这种概率题都挺感冒的=.=......还是说一下思路吧,dp[i ...
 
随机推荐
- 关于Python安装官方whl包和tar.gz包的方法详解
			
Windows环境: 安装whl包:pip install wheel -> pip install **.whl 安装tar.gz包:cd到解压后路径,python setup.py inst ...
 - day9-IO 番外
			
同步IO和异步IO,阻塞IO和非阻塞IO分别是什么,到底有什么区别?不同的人在不同的上下文下给出的答案是不同的.所以先限定一下本文的上下文. 本文讨论的背景是Linux环境下的network IO. ...
 - 关于ie6中绝对定位或浮动的div中既有向左float也有向右float时候如何让外层div自适应宽度的解决方案--
			
一个详细的说明请见: http://www.cnblogs.com/yiyang/p/3265006.html 我的问题大约为,如下代码: <!DOCTYPE html PUBLIC " ...
 - HTML5  File API解读
			
1,概述 Web应用应该具备处理广泛用户输入问题的能力,例如在Web富应用中,用户希望上传文件到服务器.File API定义了访问文件的基本操作途径,包括文件.文件列表集.错误处理等,同时,File ...
 - Android Studio 无法预览布局问题:com/android/util/PropertiesMap
			
应该是API版本太高,换成较低的就好了 API24,无法预览 换成22就没事了 Android Studio要比Eclipse好用很多,虽然Eclipse现在可以直接安装Android开发板,但AS界 ...
 - 经验总结:WebBrowser自动点击弹出提示框alert、弹出对话框confirm、屏蔽弹出框、屏蔽弹出脚本错误的解决办法
			
经验总结:WebBrowser自动点击弹出提示框alert.弹出对话框confirm.屏蔽弹出框.屏蔽弹出脚本错误的解决办法 网上有好多解决方法,可是不一定好使,本人经过多次试验,针对WebBrows ...
 - Server_id 冲突导致 IO 等待故障
			
问题描述: 线上添加新的 MySQL Slave 后,服务器异常. 1.show processlist; Queueing master event to the relay log Reconne ...
 - 关于启动MongDB的mongod.exe文件闪退的问题
			
昨天学mongdb的时候,遇到了mongod.exe闪退的问题,解决办法很简单: 你可以不执行mongod.exe,直接用命令行操作 在你安装mongdb的盘的根目录下创建一个data文件夹,一定要在 ...
 - centos7 vnc server
			
yum -y install vnc *vnc-server* vncserver vncserver :2 vncserver -geometry 1900x1024 =============== ...
 - Scala基础:闭包、柯里化、隐式转换和隐式参数
			
闭包,和js中的闭包一样,返回值依赖于声明在函数外部的一个或多个变量,那么这个函数就是闭包函数. val i: Int = 20 //函数func的方法体中使用了在func外部定义的变量 那func就 ...