水题,但是眼花了wa好几发。。。
#include <iostream>
#include <string.h>
#include <queue>
#include <vector>
#include <utility>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define maxn 210
#define LL long long
const LL INF = 100000000002;
bool visit[maxn];
int N,A,B;
typedef struct node
{
int f,up,down,step;
node(int a, int b , int c ,int d)
{
f = a;
up = b;
down = c;
step = d;
}
node(){};
}Floor;
Floor fl[maxn];
int BFS(int st, int ed)
{
memset(visit,false,sizeof(visit));
queue<Floor> Q;
Q.push(fl[st]);
visit[st] = true;
while(!Q.empty())
{
Floor temp = Q.front();
Q.pop();
if(temp.f == B) return temp.step;
int up = temp.f + temp.up;
int down = temp.f + temp.down;
if(up >= 1 && up <= N && !visit[up])
{
visit[up] = true;
if(up == ed) return temp.step +1;
else Q.push(node(up,fl[up].up,fl[up].down,temp.step+1));
}
if(down >= 1 && down <= N && !visit[down])
{
visit[down] = true;
if(down == ed) return temp.step +1;
else Q.push(node(down,fl[down].up,fl[down].down,temp.step+1));
}
}
return -1;
}
int main()
{
#ifdef xxz
freopen("in.txt","r",stdin);
#endif // xxz
while(cin>>N && N)
{
cin>>A>>B;
for(int i = 1; i <= N; i++)
{
cin>>fl[i].up;
fl[i].f = i;
fl[i].down = -1*fl[i].up;
fl[i].step = 0;
}
cout<<BFS(A,B)<<endl;
}
return 0;
}