T2385.浣熊隧道
2023-08-19 15:54:17
发布于:浙江
#include<bits/stdc++.h>
using namespace std;
struct Node{
int i,v,s;
}mp[300],t;
queue<Node>q;
int n,a,b;
bool vis[300];
int main(){
freopen("bear.in","r",stdin);
freopen("bear.out","w",stdout);
cin>>n>>a>>b;
for(int i=1;i<=n;i++){
cin>>mp[i].v;
mp[i].i=i;
}
mp[a].s=0;
q.push(mp[a]);
vis[a]=1;
int flag=-1;
int x[3];
while(!q.empty()){
t=q.front();
if(t.i==b){
flag=t.s;
break;
}
q.pop();
x[0]=t.i+t.v,x[1]=t.i-t.v;
for(int i=0;i<2;i++){
if(x[i]>=1&&x[i]<=n&&!vis[x[i]]){
vis[x[i]]=1;
q.push({x[i],mp[x[i]].v,t.s+1});
}
}
}
cout<<flag;
fclose(stdin);
fclose(stdout);
return 0;
}
这里空空如也
有帮助,赞一个