典型广搜
2024-10-24 17:38:35
发布于:广东
5阅读
0回复
0点赞
#include<bits/stdc++.h>
using namespace std;
int a[205],vis[205];
struct node{
int p,step;
};
int main(){
int n,x,y;
cin >> n >> x >> y;
for(int i = 0;i < n;i ++) cin >> a[i];
queue<node> q;
q.push({x - 1,0});
vis[x - 1] = 1;
while(!q.empty()){
int ip = q.front().p,istep = q.front().step;
q.pop();
if(ip == y - 1){
cout << istep;
return 0;
}
if(ip - a[ip] >= 0 and vis[ip - a[ip]] == 0){
vis[ip - a[ip]] = 1;
q.push({ip - a[ip],istep + 1});
}
if(ip + a[ip] <= n - 1 and vis[ip + a[ip]] == 0){
vis[ip + a[ip]] = 1;
q.push({ip + a[ip],istep + 1});
}
}
cout << -1;
return 0;
}
这里空空如也
有帮助,赞一个