代码:
#include<iostream>
using namespace std;
#include<cstring>
#include<queue>
#include<cstdio>
int main()
{
int visit[1000] = { 0 };
int n, a, b;
cin >>n >> a >> b;
int num[1000];
for (int i = 1; i <= n; i++)
{
cin >> num[i];
}
queue<int>floor;
queue<int>count;
floor.push(a); count.push(0);
visit[a] = 1;
while (!floor.empty())
{
if (floor.front() == b)
{
cout << count.front();
return 0;
}
int t = floor.front() + num[floor.front()];
if (t <= n && visit[t] == 0)
{
floor.push(t);
count.push(count.front() + 1);
visit[t] = 1;
}
t = floor.front() - num[floor.front()];
if (t >= 1 && visit[t] == 0)
{
floor.push(t);
count.push(count.front() + 1);
visit[t] = 1;
}
floor.pop();
count.pop();
}
cout << -1;
return 0;
}