AcWing
  • 首页
  • 课程
  • 题库
  • 更多
    • 竞赛
    • 题解
    • 分享
    • 问答
    • 应用
    • 校园
  • 关闭
    历史记录
    清除记录
    猜你想搜
    AcWing热点
  • App
  • 登录/注册

AcWing CCF-CSP第36次认证. 4.跳房子    原题链接    中等

作者: 作者的头像   Junimo ,  2025-06-06 17:29:46 · 四川 ,  所有人可见 ,  阅读 7


1


在一维数组上用类似BFS的思路做
再加上一些剪枝优化

优化:
1.跳到第n个格子提前结束
2.前面i+k[i]已经覆盖过的范围可以不用再跳,因此需要记录已经覆盖过的i+k[i]的最大值。

#include<iostream>
#include<queue>
using namespace std;
const int N=1e5+5;
int n;
int a[N],k[N],dis[N];
bool st[N];
void bfs(int s){
    queue<int> q;
    q.push(s);
    st[s]=1;
    int ma=2;
    while(!q.empty()&&!dis[n]){
        int t=q.front();q.pop();
        for(int i=ma;i<=t+k[t];i++){
            int e=i-a[i];
            if(!st[e]){
                q.push(e);
                st[e]=1;
                dis[e]=dis[t]+1;
            }
        }
        ma=max(ma,t+k[t]);
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin >> n;
    for(int i=1;i<=n;i++) cin >> a[i];
    for(int i=1;i<=n;i++){
        cin >> k[i];
        k[i]=min(n-i,k[i]);
    }
    bfs(1);
    if(dis[n]) cout << dis[n] << endl;
    else cout << -1 << endl;
    return 0;
}

0 评论

App 内打开
你确定删除吗?
1024
x

© 2018-2025 AcWing 版权所有  |  京ICP备2021015969号-2
用户协议  |  隐私政策  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标 qq图标
请输入绑定的邮箱地址
请输入注册信息