A
2026-05-30 11:44:27
发布于:四川
8阅读
0回复
0点赞
#include<bits/stdc++.h>
using namespace std;
const int N=210;
const int dx[2]={-1,1};
struct node{
int x,step;
};
int n,a,b;
int c[N];
queue<node> q;
bool f[205];
int main(){
cin>>n>>a>>b;
for(int i=1;i<=n;i++){
cin>>c[i];
}
if(a==b){
cout<<0;
return 0;
}
q.push(node{a,0});
f[a]=1;
while(!q.empty()){
node xx=q.front();
q.pop();
for(int i=0;i<=1;i++){
int yy=xx.x+dx[i]*c[xx.x];
if(yy>=1&&yy<=n&&!f[yy]){
if(yy==b){
cout<<xx.step+1;
return 0;
}
q.push(node{yy,xx.step+1});
f[yy]=1;
}
}
}
cout<<-1;
return 0;
}
这辈子第一次发TJ,以后参照一下大佬们的,现在只会发AC代码(嗯对
全部评论 1
这代码好长啊

2026-05-31 来自 北京
2


2026-05-31 来自 四川
1












有帮助,赞一个