这是我的代码:
#include <bits/stdc++.h>
using namespace std;
int n,st,en;
int a[205],vis[205];
struct d{
int la;
int cnt;
}m[1000101];
void bfs(){
int cl=1;
int op=1;
m[1].cnt=0;
m[1].la=st;
while(op<=cl){
int x=m[op].la+a[m[op].la];
if(x>=1&&x<=n&&vis[x]==0){
m[++cl].la=x;
m[cl].cnt=m[op].cnt+1;
vis[x]=1;
if(x==en){
cout<<m[cl].cnt;
exit(0);
}
}
int y=m[op].la-a[m[op].la];
if(y>=1&&y<=n&&vis[y]==0){
m[++cl].la=y;
m[cl].cnt=m[op].cnt+1;
vis[x]=1;
if(x==en){
cout<<m[cl].cnt;
exit(0);
}
}
op++;
}
}
int main()
{
cin>>n>>st>>en;
for(int i=1;i<=n;i++){
cin>>a[i];
}
if(st==en){
cout<<0;
return 0;
}
vis[st]=1;
bfs();
cout<<-1;
return 0;
}
下载数据后测试也没问题,有大佬能说下原因吗?