当前位置:网站首页>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;
}
边栏推荐
- Automatic test and manual test
- *Code understanding *numpy basic (plus code) that must be understood
- Full revolutionary Siamese networks for object tracking translation
- JMeter stress test index interpretation
- 【线性代数】深入理解矩阵乘法、对称矩阵、正定矩阵
- Detailed explanation of VAO
- *Yolo5 learning * data experiment based on yolo5 face combined with attention model se
- Introduction to webmethods
- Jersey2.25.1 integration freemaker
- Digital twin demonstration project -- Talking about simple pendulum (4) IOT exploration
猜你喜欢

What is NFT? An article to understand the concept of NFT

*Project recurrence * project implementation of thesis based on contextbasedemotionrecognitionusingematicdataset

OpenGL camera and periodic review

*Yolo5 learning * data experiment based on yolo5 face combined with attention model CBAM

Hcip day 8 notes

Project practice - document scanning OCR recognition

The solution of unable to import custom library in pycharm

Implement a queue with two stacks.

About the solution of thinking that you download torch as a GPU version, but the result is really a CPU version

Decision tree - ID3, C4.5, cart
随机推荐
Have you seen the interview questions of VR major? Trust me, it's absolutely useful
13. Unity2d horizontal version of two-way platform that can move up, down, left and right (two-way walking + movable + independent judgment) + random platform generation
Do you want to have a robot that can make cartoon avatars in three steps?
Appium use
Vertex buffer and shader (the cherno + leranopongl) notes
Automatic test and manual test
Digital twin demonstration project -- Talking about simple pendulum (2) vision exploration and application scenarios
One click Copy and import of web interface data into postman
Perceptron and multilayer neural network, back propagation and computational graph
The solution of unable to import custom library in pycharm
Appium doctor command error pit - resolved
Eight part essay on software testing
MS SQL Server 2019 learning
Error when using PIP: pip is configured with locations that requires tls/ssl
GBK code in idea is converted to UTF-8 format ctrl+c+v one second solution perfect solution for single code file escape
Selenium basic knowledge paging processing
Anaconda install pytorch
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
hcip第八天笔记
The difference between session and cookie