T6018.时空穿梭题解
2024-02-21 18:53:27
发布于:浙江
#include<bits/stdc++.h>
using namespace std;
int a[205];
struct Node{
int x;
int d;
};
int vis[205];
int main(){
int n,m,k;
cin>>n>>m>>k;
for(int i=1;i<=n;i++) cin>>a[i];
queue <Node> q;
q.push({m,0});
vis[m]=true;
while(!q.empty()){
Node t=q.front();
q.pop();
if(t.x==k){
cout<<t.d;
return 0;
}
int tx;
tx=t.x+a[t.x];
if(tx<=n && !vis[tx]){
q.push({tx,t.d+1});
}
tx=t.x-a[t.x];
if(tx>=1 && !vis[tx]){
q.push({tx,t.d+1});
vis[tx]=true;
}
}
cout<<-1;
return 0;
}
这里空空如也
有帮助,赞一个