#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
typedef struct Node
{
int cf, stp;
}Node;
int main()
{
int n, a, b, k[202], ans, vid[202];
while (cin>>n, n)
{
cin>>a>>b;
for (int i = 1; i <= n; i++)
cin>>k[i];
queue<Node> que; //我去~之前把队列定义到while循环外面了,导致队列没有清空,所以WA了。。。。&_&
memset(vid, 0, sizeof(vid));
Node p;
p.cf = a;
p.stp = 0;
que.push(p);
vid[a] = 1;
ans = -1;
while (!que.empty())
{
Node h = que.front();
que.pop();
if (h.cf == b)
{
ans = h.stp;
break;
}
int up = h.cf+k[h.cf];
if (up <= n && !vid[up])
{
Node q;
q.cf = up;
q.stp = h.stp+1;
que.push(q);
vid[up] = 1;
}
int down = h.cf-k[h.cf];
if (down > 0 && !vid[down])
{
Node q;
q.cf = down;
q.stp = h.stp+1;
que.push(q);
vid[down] = 1;
}
}
cout<<ans<<endl;
}
}
hdu 1548 A strange lift
最新推荐文章于 2019-07-25 19:43:03 发布