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
",cas++,dp[1]);
38     }
39 
40     return 0;
41 }
View Code
原文地址:https://www.cnblogs.com/-maybe/p/4521143.html