@[LikeMiracle](/user/731994) 我以前写的DFS,不行,爆掉,~~我太菜了,所以~~写BFS,就过了
by dzsf_lhz @ 2024-03-16 08:20:08
```c
#include<iostream>
#include<queue>
using namespace std;
int a[201],v[201];
int n,b,c;
struct point {
int x,step;
point() {
}
point(int xx,int st) {
x=xx;
step=st;
}
};
int bfs() {
v[b]=1;
queue<point> qq;
qq.push(point(b,0));
while(!qq.empty()) {
point w=qq.front();
qq.pop();
int x=w.x,e=w.step;
if(x==c) return w.step;
if(x+a[x]>=1&&x+a[x]<=n&&v[x+a[x]]==0) {
qq.push(point(x+a[x],e+1));
v[x+a[x]]=1;
}
if(x-a[x]>=1&&x-a[x]<=n&&v[x-a[x]]==0) {
qq.push(point(x-a[x],e+1));
v[x-a[x]]=1;
}
}
return -1;
}
int main() {
cin>>n>>b>>c;
for(int i=1; i<=n; i++) cin>>a[i];
cout<<bfs();
return 0;
}
```
by xuehanlin @ 2024-03-26 16:58:54