服了
2023-07-29 10:10:17
发布于:广东
有个shaboi上课不抄代码
#include <bits/stdc++.h>
using namespace std;
int n,a,b;
bool vis[208];
int mp[208];
struct Node{
int step;
int x;
};
int bfs(){
queue <Node> q;
q.push({0,a});
vis[a] = true;
while(!q.empty()){
Node fro = q.front();
q.pop();
if(fro.x == b){
return fro.step;
}
int tl = fro.x - mp[fro.x];
int tr = fro.x + mp[fro.x];
if(tl >= 1 && tl <= n && !vis[tl]){
vis[tl] = true;
q.push({fro.step+1,tl});
}
if(tr >= 1 && tr <= n && !vis[tr]){
vis[tr] = true;
q.push({fro.step+1,tr});
}
}
return -1;
}
int main(){
freopen("bear.in","r",stdin);
freopen("bear.out","w",stdout);
cin >> n >> a >> b;
for(int i=1;i<=n;i++) cin >> mp[i];
cout << bfs() << endl;
fclose(stdin);
fclose(stdout);
return 0;
}
这里空空如也
有帮助,赞一个