当前位置:网站首页>P1135 奇怪的电梯题解
P1135 奇怪的电梯题解
2022-07-24 07:56:00 【bj_hacker】
题目
链接
https://www.luogu.com.cn/problem/P1135
字面描述
题目描述
呵呵,有一天我做了一个梦,梦见了一种很奇怪的电梯。大楼的每一层楼都可以停电梯,而且第 ii 层楼(1 \le i \le N1≤i≤N)上有一个数字 K_iK
i
(0 \le K_i \le N0≤K
i
≤N)。电梯只有四个按钮:开,关,上,下。上下的层数等于当前楼层上的那个数字。当然,如果不能满足要求,相应的按钮就会失灵。例如: 3, 3, 1, 2, 53,3,1,2,5 代表了 K_iK
i
(K_1=3K
1
=3,K_2=3K
2
=3,……),从 11 楼开始。在 11 楼,按“上”可以到 44 楼,按“下”是不起作用的,因为没有 -2−2 楼。那么,从 AA 楼到 BB 楼至少要按几次按钮呢?
输入格式
共二行。
第一行为三个用空格隔开的正整数,表示 N, A, BN,A,B(1 \le N \le 2001≤N≤200,1 \le A, B \le N1≤A,B≤N)。
第二行为 NN 个用空格隔开的非负整数,表示 K_iK
i
。
输出格式
一行,即最少按键次数,若无法到达,则输出 -1。
输入输出样例
输入 #1复制
5 1 5
3 3 1 2 5
输出 #1复制
3
说明/提示
对于 100 %100% 的数据,1 \le N \le 2001≤N≤200,1 \le A, B \le N1≤A,B≤N,0 \le K_i \le N0≤K
i
≤N。
代码实现
#include<bits/stdc++.h>
using namespace std;
const int maxn=200+10;
int n,st,ed,flag;
int a[maxn],que[maxn],dis[maxn];
bool vis[maxn];
bool inbound(int x){
return x>=1&&x<=n;}
int main(){
scanf("%d%d%d",&n,&st,&ed);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
int head=0,tail=0;
vis[st]=true;
que[tail++]=st;
while(head<tail){
int x=que[head++];
if(x==ed){
flag=1;
break;
}
if(inbound(x+a[x])&&!vis[x+a[x]]){
vis[x+a[x]]=true;
que[tail++]=x+a[x];
dis[x+a[x]]=dis[x]+1;
}
if(inbound(x-a[x])&&!vis[x-a[x]]){
vis[x-a[x]]=true;
que[tail++]=x-a[x];
dis[x-a[x]]=dis[x]+1;
}
}
if(!flag)printf("-1\n");
else printf("%d\n",dis[ed]);
return 0;
}
边栏推荐
- Thesis reading: geotransformer
- 【线性代数】深入理解矩阵乘法、对称矩阵、正定矩阵
- Stable TTL serial port rate supported by Arduino under different dominant frequencies
- Appium doctor command error pit - resolved
- Selenium basic knowledge debugging method
- 【MATLAB】(四)MATLAB在线性代数中的应用
- Math。 Round, numeric rounding, underlying code parsing
- 多种优化方法打印100~200之间的素数
- Hcip day 7
- The difference between online learning and offline learning
猜你喜欢

Use JMeter to analyze and test the lottery probability of the lottery interface

简易网闸-内网服务器安全获取外网数据

Perceptron and multilayer neural network, back propagation and computational graph
![2022-07-23: given n items, each item has weight (w[i]) and value (v[i]), only two items can be selected at most, and the weight does not exceed bag. What is the maximum return value? N <= 10^5, w[i] <](/img/f4/ba2706e93f042dd8b110fac0d873c8.png)
2022-07-23: given n items, each item has weight (w[i]) and value (v[i]), only two items can be selected at most, and the weight does not exceed bag. What is the maximum return value? N <= 10^5, w[i] <

Opencv project practice - credit card recognition

NFT是什么?一篇文章搞懂NFT的概念

Debug NO2 check for errors according to the process

Install librosa using Tsinghua image

MySQL 啥时候用表锁,啥时候用行锁?

Learning to track at 100 FPS with deep progression networks
随机推荐
Installation and use of CONDA
Detailed explanation of VAO
The vision group of Hegong University Sky team trained day3 - machine learning, strengthened the use of Yolo models, and learned pumpkin books and watermelon books
Solve the problem that Anaconda navigator cannot be opened
2021-06-03 database query - sorting
Selenium basic knowledge automatically login Baidu Post Bar
Automatic test and manual test
【sklearn】tree.DecisionTreeClassifier
[sklearn] RF cross validation out of bag data parameter learning curve grid search
Jetson AgX Orin source change
The solution of unable to import custom library in pycharm
App performance test case
Basic operation of queue
(dkby) DFL learning notes
Workspace creation
Qt|字符串生成二维码功能
Function analysis of e-commerce website development and construction
Selenium basic knowledge automatically login Baidu online disk
Intelligent robots and intelligent systems (Professor Zheng Zheng of Dalian University of Technology) -- 2. Mobile Robot Perception
Tools for data visualization