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

题目大意:n个格子,每个格子有一个值。从1开始,每次扔6个面的骰子,扔出几点就往前几步,然后把那个格子的金子拿走;如果扔出的骰子+所在位置>n,就重新扔,直到在n; 问取走这些值的期望值是多少

解题思路:【1】 【2】 【3】【4】 【5】 【6】 【7】 【8】 【9】//格子和值都是一样,所以下述的话,值就是格子,格子就是值。。。 比如这样的9个格子,我们总底往上来对于第9个格子,因为只有9,能取的期望就是9;对于第8个格子,8是一定要取的,而9也是一定回取的,所以对于第8个格子,期望就是17;对于第7个格子,7是一定要取的,对于后面可能是直接取了9,或者先取8再取9,情况是满足,对于每种情况概率是1/2,所以就是7+9/2+(8+9)/2=20;
PS:上面的情况,在7后面的时候,我们可能取9,或者先取8,那么其实就是拿了第8个格子的期望和第9个格子期望,期望就是能取的值,然后*概率,全部情况的总和就是新的期望,有人会奇怪那7呢?我们的前提是对于第7格一定拿7啊;对于第6个格子,那么就是6一定要拿的,然后会拿7,拿8,拿9,他们的期望*概率的总和+他能取的值就是6的第6个格子的期望...以此类推;对于概率的其实一想更简单...我们一开始就在1,概率就是1,然后扔一个骰子对于每个面的概率就是1/6,那么dp[i]代表概率,每次对能到达的地方更新概率,最后期望就是值乘以概率的总和+1,1是一定要取的。

题目链接:http://fastvj.rainng.com/problem/LightOJ-1030


/*
@Author: Top_Spirit
@Language: C++
*/
//#include 
#include 
#include 
#include 
#include 
#include 
using namespace std ;
typedef long long ll ;
const int Maxn = 1e5 + 10 ;
const int Maxn1 = 1e6 + 10 ;
const int INF = 0x3f3f3f3f ;

double dp[Maxn] ;
double ans ;

int main (){
    int T, Cas = 0 ;
    cin >> T ;
    while (T--){
        int n, k ;
        double tmp = 0 ;
        cin >> n ;
        for (int i = 1; i <= n; i++){
            cin >> dp[i] ;
        }
        for (int i = n - 1; i >= 1; i--){
            tmp = 0 ;
            k = min(6, n - i ) ;
//            cout << "++++" << k << endl ;
            for (int j = i + 1; j < i + 1 + k; j++){
//                cout << "j = " << j << " " << dp[j] << endl ;
                tmp += dp[j] ;
            }
//            cout << "-----" << tmp << endl ;
            dp[i] = dp[i] + tmp/ double (k) ;
        }
        cout << fixed << setprecision(10) ;
        cout << "Case " << ++Cas << ": " << dp[1] << endl ;
    }
    return 0 ;
}

 

你可能感兴趣的:(期望&概率,DP,acm,概率DP)