B - Discovering Gold

本文探讨了一个有趣的算法问题,即在一个由多个格子组成的洞穴中,通过掷骰子前进并收集黄金,求达到终点时的黄金期望值。文章详细解释了为什么简单动态规划方法不适用,并提出了正确的求解思路,最后附上了实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B - Discovering Gold

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~N,每个格子下面有黄金,每到一个格子就掷骰子,掷到的点数就是你下一次跳跃的距离,骰子的有6面,点数分别为1~6,每次掷的点数是等概率的,如果你掷色子使你跳到第N格外面,则重新掷骰子。问到达n点的所获得黄金的期望是多少?

分析:

不能简单的用DP[i]代表1到i的期望不能简单的用DP[i]代表1到i的期望DP[i]1i 如果对之有疑问请看下面,否则建议跳过

用dp[n]代表1到n点所获的期望的话,则不能单纯的让前置位置的期望加上该点的黄金再乘以的到该状态的概率.用dp[n]代表1到n点所获的期望的话,则不能单纯的让前置位置的期望加上该点的黄金再乘以的到该状态的概率.dp[n]1n.

对于dp[j]dp[j]dp[j],jjj的每个前置顶点iii加进来的期望为dp[j]+=(w[1 to i]+val[i to j])∗P(1 to i)∗P(i to j)dp[j]+=(w[1~to~i]+val[i\ to\ j ])*P(1\ to\ i)*P(i\ to \ j)dp[j]+=(w[1 to i]+val[i to j])P(1 to i)P(i to j)

而P(1 to i)!=1而P(1\ to\ i)!=1P(1 to i)!=1 ,因为1去的方向最终点不全在i点,即所有可能的最终点不在i出,因为1去的方向最终点不全在i点,即所有可能的最终点不在i出,1ii

用dp[i]代表i到n点的期望,那么dp[i]的求法:用dp[i]代表i到n点的期望,那么dp[i]的求法:dp[i]indp[i]:

dp[i]=∑j(val[i]+dp[j])∗P(i to j)​dp[i]=\sum _{j}(val[i]+dp[j])*P(i\ to\ j)​dp[i]=j(val[i]+dp[j])P(i to j)

注意 P(i to j)的求法即可注意\ P(i\ to\ j)的求法即可 P(i to j)

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1e2+10;
int val[maxn];
double dp[maxn];
int main()
{
    int t,n,cas=0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;++i)
            scanf("%d",val+i);
        dp[n]=val[n];
        int tot=0;
        for(int i=n-1;i>0;--i)
        {
            tot=min(i+6,n)-i;//可供选择的个数
            dp[i]=val[i];
            for(int j=i+tot;j>i;--j)
                dp[i]+=1.0/tot*dp[j];
        }
        printf("Case %d: %.6f\n",++cas,dp[1]);
    }
}

##如果用DP[i]表示1到i的期望,则代码这样写

include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1e2+10;
int val[maxn];
double dp[maxn];
double pp[maxn];
int main()
{
    int t,n,cas=0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;++i)
            scanf("%d",val+i);
        int tot;
        dp[1]=val[1];
        pp[1]=1;
        for(int i=2;i<=n;++i)
        {
            dp[i]=0;
            pp[i]=0;
            for(int j=max(i-6,1);j<i;j++)
            {
                 tot=min(j+6,n)-j;
                 dp[i]+=(dp[j]+val[i]*pp[j])*1.0/tot;
                 pp[i]+=pp[j]*1.0/tot;
            }
        }
        printf("Case %d: %.6f\n",++cas,dp[n]);
    }
    return 0;
}
<think>我们正在处理用户关于bluetoothctl命令的问题。用户提到使用`bluetoothctl show`命令时设备没有进行发现(discovering)的问题。实际上,`bluetoothctl show`命令用于显示蓝牙适配器的属性,而不是控制设备发现。要启用设备发现,我们需要使用其他命令。 关键点: 1. 显示蓝牙适配器信息:`bluetoothctl show [适配器地址]`(默认是第一个适配器) 2. 开启蓝牙适配器的可发现模式:`bluetoothctl discoverable on` 3. 开启蓝牙适配器的扫描(发现)模式:`bluetoothctl scan on`(用于发现其他设备) 但是,用户的问题可能是想让自己的设备被其他设备发现(即可发现模式),或者是想要扫描周围的设备(即扫描模式)。根据用户描述“not discovering devices”,可能是指无法扫描到其他设备。 步骤: 1. 确保蓝牙适配器已打开:`bluetoothctl power on` 2. 开启扫描:`bluetoothctl scan on` 3. 开启可发现模式(如果需要被其他设备发现):`bluetoothctl discoverable on` 另外,如果用户想要扫描设备,但扫描不到,可能的原因有: - 蓝牙适配器未启用 - 设备不在范围内或未处于可发现状态 - 可能需要使用`agent on`和`default-agent`来设置代理,以便处理配对请求 因此,我们给出以下步骤: 1. 打开蓝牙控制台:`bluetoothctl` 2. 打开电源:`power on` 3. 设置代理:`agent on`,然后`default-agent` 4. 开始扫描:`scan on` 5. 等待扫描结果,使用`devices`命令列出发现的设备 如果问题仍然存在,可以尝试重启蓝牙服务:`sudo systemctl restart bluetooth` 注意:在某些系统中,可能需要先停止扫描才能进行配对等操作。 现在,我们按照要求生成回答,并注意格式要求。</think>### 使用 bluetoothctl 命令启用设备发现功能 您遇到的问题可能源于混淆了 `show` 和 `discoverable` 命令的功能: 1. `bluetoothctl show` 仅显示适配器信息(如 MAC 地址、名称等),**不会启用发现功能** 2. 正确的设备发现流程如下: #### 步骤 1:启动蓝牙控制台 ```bash bluetoothctl ``` #### 步骤 2:开启蓝牙适配器电源 ```bash power on ``` #### 步骤 3:启用设备可发现模式(关键步骤) ```bash discoverable on # 允许其他设备发现本机 ``` #### 步骤 4:启动扫描(发现其他设备) ```bash scan on # 主动扫描周围设备 ``` #### 步骤 5:查看发现的设备 ```bash devices # 列出所有发现的设备 ``` #### 常见问题排查: 1. **硬件检查** - 执行 `list` 确认蓝牙适配器是否被识别 - 无输出时需检查硬件驱动[^1] 2. **服务状态** 重启蓝牙服务: ```bash sudo systemctl restart bluetooth ``` 3. **权限问题** 添加用户到蓝牙组: ```bash sudo usermod -aG bluetooth $USER ``` 4. **持续扫描** 若需后台持续扫描: ```bash bluetoothctl --timeout=0 scan on ``` > 注:设备发现需双方均开启可发现模式,部分设备(如蓝牙鼠标)仅在配对时短暂可见[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值