浣熊隧道
2023-08-19 10:46:50
发布于:浙江
#include<bits/stdc++.h>
using namespace std;
int n,k[220],a,b,vis[220];
struct node{
int y,step;
};
int flag;
void bfs(){
queue<node> q;
q.push({a,0});
vis[a]=1;
while(!q.empty()){
if(q.front().y==b){
flag=1;
cout<<q.front().step;
return;
}
int xx=q.front().y+k[q.front().y];
if(xx>=1 && xx<=n && vis[xx]==0){
q.push({xx,q.front().step+1});
vis[xx]=1;
}
int xx2=q.front().y-k[q.front().y];
if(xx2>=1 && xx2<=n && vis[xx2]==0){
q.push({xx2,q.front().step+1});
vis[xx2]=1;
}
q.pop();
}
}
int main(){
freopen("bear.in","r",stdin);
freopen("bear.out","w",stdout);
cin>>n>>a>>b;
for(int i=1;i<=n;i++)
cin>>k[i];
bfs();
if(flag==0) cout<<-1;
fclose(stdin);
fclose(stdout);
return 0;
}
这里空空如也
有帮助,赞一个