其他分享
首页 > 其他分享> > Discovering Gold(期望DP)

Discovering Gold(期望DP)

作者:互联网

Discovering Gold

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

题意:有一个迷宫是1×n的格子,一个人每到一个格子就可以把这个格子内的金子全部拿走,刚开始站在第1个格子,然后开始掷骰子得到点数x,他就要从当前位置走到加x的位置,如果发现位置是大于n的就重新掷骰子直到符合,如果他到了第n个格子就可以结束了。问这个人从迷宫里得到的金子的期望是多少。

题解:

  期望DP 从后往前推 这个题和HDU - 4405 的区别在于这个题最后一步必须走到n位置,而HDU - 4405要求最后一步走到的位置大于等于n即可。所以本题需要判断上一步的位置是否已经超过了N.

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 int a[110];
 7 double dp[110];
 8 int n;
 9 int casen,ca=1;
10 int main()
11 {
12     scanf("%d",&casen);
13     while(casen--)
14     {
15         memset(dp,0,sizeof(dp));
16         scanf("%d",&n);
17         for(int i=1;i<=n;i++)
18         {
19             scanf("%d",&a[i]);
20         }
21         dp[n]=a[n];
22         for(int i=n-1;i>=1;i--)
23         {
24             for(int j=1;j<=6;j++)
25             {
26                 double len=min(6,n-i);//这是唯一区别
27                 dp[i]+=dp[i+j]*(double)(1.0/len);
28             } 
29             dp[i]+=a[i];
30         }
31         printf("Case %d: %.8f\n",ca++,dp[1]);
32     }
33 }

 

标签:Gold,gold,int,cave,will,integer,Discovering,position,DP
来源: https://www.cnblogs.com/1013star/p/10361455.html