LightOJ-1030-Discovering Gold
链接:
https://vjudge.net/problem/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.
思路:
DP[i] 表示从i点往n点走的期望值,考虑从某点开始时可以直接拿走对应的黄金。
同时i点往前延伸min(6, n-i)个点都是有概率走到的,直接计算期望。
代码:
#include <iostream>
#include <memory.h>
#include <string>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <algorithm>
#include <map>
#include <queue>
#include <math.h>
#include <cstdio>
#include <set>
#include <iterator>
#include <cstring>
#include <assert.h>
using namespace std;
typedef long long LL;
double Dp[110];
int a[110];
int n;
int main()
{
int t, cnt = 0;
scanf("%d",&t);
while (t--)
{
memset(Dp, 0, sizeof(Dp));
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];
int step = min(6, n-i);
for (int j = 1;j <= step;j++)
Dp[i] += (1.0/step)*Dp[i+j];
}
printf("Case %d: %.7lf\n", ++cnt, Dp[1]);
}
return 0;
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步