首页 技术 正文
技术 2022年11月18日
0 收藏 776 点赞 4,692 浏览 1099 个字

题目链接:https://www.luogu.org/problemnew/show/P1135

题目描述

呵呵,有一天我做了一个梦,梦见了一种很奇怪的电梯。大楼的每一层楼都可以停电梯,而且第 i 层楼 (1≤i≤N) 上有一个数字Ki​(0≤Ki​≤N) 。电梯只有四个按钮:开,关,上,下。上下的层数等于当前楼层上的那个数字。当然,如果不能满足要求,相应的按钮就会失灵。例如:3,3,1,2,5 代表了Ki​(K1​=3,K2​=3,…) ,从 1 楼开始。在 1楼,按“上”可以到 4 楼,按“下”是不起作用的,因为没有 −2 楼。那么,从 A 楼到 B 楼至少要按几次按钮呢?

输入格式:

共二行。

第一行为 3 个用空格隔开的正整数,表示N,A,B(1≤N≤200,1≤A,B≤N) 。

第二行为 N 个用空格隔开的非负整数,表示Ki​ 。

输出格式:

一行,即最少按键次数,若无法到达,则输出 −1 。

输入样例#1: 

5 1 5
3 3 1 2 5

输出样例#1: 

3裸的bfs
#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
int n, a, b;
struct node
{
int x, step;
};
int arr[];
int vis[];
queue<node>q;int bfs()
{
node now, next;
while (!q.empty())
{
now = q.front();
q.pop();
if (now.x == b)
{
return now.step;
}
if (now.x + arr[now.x] <= && !vis[now.x + arr[now.x]]) //向上坐电梯
{
next.x = now.x + arr[now.x];
vis[next.x] = ;
next.step=now.step+;
q.push(next);
}
if (now.x - arr[now.x] >= && !vis[now.x - arr[now.x]]) //向下坐电梯
{
next.x = now.x - arr[now.x];
vis[next.x] = ;
next.step = now.step + ;
q.push(next);
}
}
return -;
}int main()
{
cin >> n >> a >> b;
memset(vis, , sizeof(vis));
for (int i = ; i <= n; i++)
scanf("%d", &arr[i]);
node now; now.x =a , now.step = ;
q.push(now);
int ans=bfs();
cout << ans << endl;
return ;
}
2018-05-31
上一篇: 关于 C++ STL
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,023
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,360
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,143
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,774
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,852