Discovering Gold LightOJ - 1030 (probability 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

 

The meaning of the question: There is a 1*n cave. At the beginning, you are in the first square. You throw a six-sided sieve. When you throw it to n, you will go to the nth square in front of your current position, and then you can get The number of gold stored in this grid (throw a sieve every time a new grid is reached).

The gold number for each square has been given, ask your expectation of the total gold number you can get by walking to the last square.

 

Idea: The sieve has only 6 numbers 1-6, so you can only walk to one of the 6 squares before the current square in each step. Assuming that the expected value that can be obtained by walking to the current square is dp[i], then dp[ i] = ( dp[i+1]/6 + ... + dp[i+6]/6 ) + dp[i].

Think about it, the farther you are from the end, the more gold you can theoretically get, because you can take more steps, so the closer you are to the starting point, the higher the dp value. The initial value of dp[i] is the number of golds in the i-th grid, and then add the amount of gold you can go to the next position.

The expectation of the gold obtained, there are six possibilities for the next position, each of which is 1/6; of course, there are special cases, that is, the distance between your current position and the end point is less than 6, then special consideration, there are several The denominator of the recurrence formula above a grid is a few.

From the end point forward, the last value of dp[1] is the answer.

Look at the code below to understand it (although I also seem to understand YwY).

 

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cstdio>
 5 using namespace std;
 6 int main()
 7 {
 8     int t,n,cnt=0;
 9     double a[1007],dp[1007];
10     cin>>t;
11     while(t--)
12     {
13         cin>>n;
14         memset(dp,0,sizeof(dp));
15         for(int i=1; i<=n; i++)
16             cin>>a[i];
17         dp[n] += a[n];
18         for(int i=n-1;i>=1;i--)
19         {
20             dp[i] += a[i];//当前点的期望等于后6个点的期望的 1/6 的和; 
21             int len = min(n-i,6);//判断当前点之后是否还有6个点 ,不够六个点则有几个算几个。
22             for(int j=i+1; j<=i+6 && j<=n; j++)
23             {
24                 dp[i] += dp[j]/len;
25             }
26         }
27         cout<<"Case "<<++cnt<<": ";
28         printf("%.7lf\n",dp[1]);
29     }
30     return 0;
31  }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325217141&siteId=291194637