LightOJ1030 Discovering Gold 概率DP 水题

Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Description

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,每个格子有一定的金子

现在你从1开始,每次用一个骰子,扔出的数字为多少就前进多少步,同时把该位置的金子也拿了。

求到达n时期望的金子数。

注意:如果前进的位置超出了n,则重新扔骰子。(刚开始这里理解错了)

概率DP

dp[i]表示从位置i到n期望的金子数,即拿到金子的期望值。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4
 5 using namespace std;
 6
 7 const int maxn=120;
 8
 9 double dp[maxn];
10 double w[maxn];
11
12 int main()
13 {
14     int test;
15     scanf("%d",&test);
16     int cas=1;
17     while(test--)
18     {
19         int n;
20         scanf("%d",&n);
21         for(int i=1;i<=n;i++)
22             scanf("%lf",&w[i]);
23
24         for(int i=1;i<=n+10;i++)
25             dp[i]=0.0;
26
27         dp[n]=w[n];
28         for(int i=n-1;i>0;i--)
29         {
30             double tmp=0.0;
31             int cnt=min(6,n-i);
32             for(int j=1;j<=cnt;j++)
33                 tmp+=dp[i+j];
34             dp[i]=tmp/(double)cnt+w[i];
35         }
36
37         printf("Case %d: %.10f\n",cas++,dp[1]);
38     }
39
40     return 0;
41 }

时间: 2024-10-07 21:16:46

LightOJ1030 Discovering Gold 概率DP 水题的相关文章

Codeforces 148D 一袋老鼠 Bag of mice | 概率DP 水题

除非特别忙,我接下来会尽可能翻译我做的每道CF题的题面! Codeforces 148D 一袋老鼠 Bag of mice | 概率DP 水题 题面 胡小兔和司公子都认为对方是垃圾. 为了决出谁才是垃圾,大哥拿来了一袋老鼠,其中有w只白老鼠和b只黑老鼠.胡小兔先抓,先抓到白老鼠的人赢. 每次学姐抓完老鼠之后,总会有另外一只老鼠从袋子里自己跑出来(这只老鼠不算任何人抓的),而胡小兔抓老鼠时则不会发生这样的事. 每次袋子里的每只老鼠被抓到的概率相等,当有一只老鼠跑出来的时候,每只老鼠跑出来的几率也相

13年山东省赛 The number of steps(概率dp水题)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud The number of steps Time Limit: 1 Sec  Memory Limit: 128 M Description Mary stands in a strange maze, the maze looks like a triangle(the first layer have one room,the second layer have two ro

hdu3722Card Game 概率dp水题

//3中天气前一天天气为i转为第二天天气为j的概率为p[i][j] //问第一天天气为i,n天后天气为j的概率 //dp[i][j][k]在第一天天气为j的情况下第n天的天气为j的概率 //dp[i][j][k] += dp[i-1][j][s]*dp[1][s][k] ; #include<cstdio> #include<cstring> #include<iostream> using namespace std ; const int maxn = 1010 ;

poj 3071 Football (概率DP水题)

G - Football Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description Consider a single-elimination football tournament involving 2n teams, denoted 1, 2, …, 2n. In each round of the tournament, all teams

HDU 4576 Robot 概率DP 水题

Robot Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)Total Submission(s): 3851    Accepted Submission(s): 1246 Problem Description Michael has a telecontrol robot. One day he put the robot on a loop with n cells.

hdu4405Aeroplane chess 概率dp水题

//从0到n有n+1个格子 //对于格子i,掷一次骰子的数为x.那么能够从位置i到位置i+x //格子之间有连线,假设格子a和b有连线,那么从a到b不用掷骰子 //求从0到n的骰子掷的次数的期望 //dp[i] = 1/6*segma(dp[k]) + 1 (i<=k<=i+6) #include<cstdio> #include<iostream> #include<cstring> using namespace std ; const int maxn

Light OJ 1030 - Discovering Gold(概率dp)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1030 题目大意:有一个很长的洞穴, 可以看做是1-n的格子.你的起始位置在1的地方, 每个格子中都有价值为v[i]的宝藏. 有一个6面的骰子,数字为从1-6, 每次摇一次骰子, 得到的数字x后, 你可以到达距离当前位置大x的位置, 并且得到那个位置的宝藏. 如果要走的位置在n的外面, 那么在此摇骰子, 直到找到一个合适的数字.到达n位置的时候结束. 现在想知道走到n位置的能够

HDU 4405 Aeroplane chess 概率DP 水题

Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2327    Accepted Submission(s): 1512 Problem Description Hzz loves aeroplane chess very much. The chess map contains N+1 grids lab

hdu 5001 walk 概率dp入门题

Description I used to think I could be anything, but now I know that I couldn't do anything. So I started traveling. The nation looks like a connected bidirectional graph, and I am randomly walking on it. It means when I am at node i, I will travel t