LightOj-1030 Discovering Gold (期望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

题解:

起始位置是1,从1走到n,给你一个骰子(6个面),按点数走,收集每一点上的金子,如果你将要走到的位置在n之内,就继续扔,往前走,如果在n之外,就一直扔到合适的位置为止,求到达n点时的期望

 这个题是一个求期望的题,那么值得注意的是,当扔在n之外的情况是无效的,所以我们在位置i<n6i<n−6的时候 此时的概率应该为 1/(n-i),当我们在算权值的时候,我们发现对于位置i来说,它可以到i+1,i+2,i+6i+1,i+2,……,i+6 这些点,而这些点的权值又与他们后面6个点相关,因此我们倒过来从最后一个点开始求,最后一个点是一定会取的,于是我们就用一个dp数组把他计算一下;

参考代码为:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int T,n;
 4 double dp[110];
 5 
 6 int main()
 7 {
 8     scanf("%d",&T);
 9     for(int k=1;k<=T;k++)
10     {
11         scanf("%d",&n);
12         memset(dp,0,sizeof dp);
13         for(int i=1;i<=n;i++) scanf("%lf",dp+i);
14         for(int i=n-1;i>=1;i--)
15         {
16             for(int j=1;j<=6;j++) dp[i]+=dp[i+j]/(1.0*min(6,n-i));
17         }
18         printf("Case %d: %.10lf\n",k,dp[1]);
19     }
20     
21     return 0;
22 }
View Code

 

转载于:https://www.cnblogs.com/songorz/p/9483740.html

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值