LightOJ 1030 Discovering Gold(概率DP)题解

题意:1~n每格都有金子,每次掷骰子,掷到多少走几步,拿走那格的金子,问你金子的期望

思路:dp[i]表示从i走到n金子的期望,因为每次最多走1<=x<=6步,所以dp[i] = a[i] + sum(1 / x * dp[j])

代码:

#include<set>
#include<map>
#include<stack>
#include<cmath>
#include<queue>
#include<vector>
#include<string>
#include<cstdio>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
typedef long long ll;
using namespace std;
const int maxn = 100 + 10;
const int MOD = 998244353;
const int INF = 0x3f3f3f3f;
int a[maxn];
double dp[maxn];    //从i开始走到n的期望
int main(){
    int n, ca = 1, t;
    scanf("%d", &t);
    while(t--){
        scanf("%d", &n);
        for(int i = 1; i <= n; i++)
            scanf("%d", &a[i]);
        dp[n] = a[n];
        for(int i = n - 1; i >= 1; i--){
            dp[i] = a[i];
            for(int j = i + 1; j <= min(i + 6, n); j++){
                dp[i] += 1.0 / min(6, n - i) * dp[j];
            }
        }
        printf("Case %d: %.8lf\n", ca++, dp[1]);
    }

    return 0;
}

 

posted @ 2019-03-09 13:54  KirinSB  阅读(231)  评论(0编辑  收藏  举报