topic description
Hehe, one day I had a dream, dreaming of a very strange elevator. Elevators can be stopped on each floor of the building, and there is a number Ki(0≤Ki≤N) on the i-th floor (1≤i≤N). The elevator has only four buttons: open, close, up, down. The number of floors up and down is equal to the number on the current floor. Of course, if the requirements are not met, the corresponding button will be disabled. For example: 3, 3, 1, 2, 5 represent Ki(K1=3, K2=3,…), starting from floor 1. On the 1st floor, press "up" to go to the 4th floor, press "down" will not work, because there is no −2 floor. So, what is the minimum number of button presses required to get from Building A to Building B?
input format
There are two lines in total.
The first line is three positive integers separated by spaces, representing N, A, B (1≤N≤200, 1≤A, B≤N).
The second line is N non-negative integers separated by spaces, representing Ki.
output format
One line, that is, the minimum number of key presses, if it cannot be reached, it will be output
-1
.Input and output samples
enter
5 1 5 3 3 1 2 5output
3Instructions/Tips
For 100% of the data, 1≤N≤200, 1≤A,B≤N, 0≤Ki≤N.
#include<bits/stdc++.h>
using namespace std;
int n,a,b,e[210],vis[210];
int main()
{
cin>>n>>a>>b;
for(int i=1;i<=n;i++)
{
cin>>e[i];
}
queue<int>f;
queue<int>s;
f.push(a);
s.push(0);
vis[a]=1;
while(!s.empty())
{
if(f.front()==b)
{
cout<<s.front()<<endl;
return 0;
}
int t=f.front()+e[f.front()];//上升楼层
if(t<=n&&vis[t]==0)
{
f.push(t);
s.push(s.front()+1);
vis[t]=1;
}
t=f.front()-e[f.front()];//下降楼层
if(t>=1&&vis[t]==0)
{
f.push(t);
s.push(s.front()+1);
vis[t]=1;
}
f.pop();
s.pop();
}
cout<<-1<<endl;
return 0;
}