ACM 训练日志记录 · 思考 · 成长 提交 / 修改记录
王梓豪 / 题目列表训练档案
导出

蹦蹦菇

来源:其他 ★ 1300

本题活力0.26按难度、完成结果与训练证据估算

算法如山行则将至

题目描述

熊二需最少跳跃次数到达第n个蹦蹦菇,每次可跳k或p距离。

代码

#include <bits/stdc++.h>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n,p0;
    cin>>n>>p0;
    vector<int> p(n + 1);
    for (int i = 1; i < n; i++) {
        cin >> p[i];
    }
    queue<int> q;
    vector<int> if_v(n+1,INT_MAX);
    if_v[0]=0;
    for(int k=1;k<=p0;k++)
    {
        if_v[k]=1;
        q.push(k);
    }
        while(!q.empty())
        {
            int temp=q.front();
            if(temp==n)break;
            q.pop();
            if(temp+p[temp]<=n&&if_v[temp+p[temp]]==INT_MAX){if_v[temp+p[temp]]=if_v[temp]+1;q.push(temp+p[temp]);}
            if(temp-p[temp]>0&&if_v[temp-p[temp]]==INT_MAX){if_v[temp-p[temp]]=if_v[temp]+1;q.push(temp-p[temp]);}
        }
    if(if_v[n]!=INT_MAX)cout<<if_v[n];
else cout<<-1;
}