没函数题解
2025-05-10 09:49:29
发布于:香港
10阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
int a[205];
bool vis[205];
struct Node {
int x;
int step;
};
int main() {
int n, s, e;
cin >> n >> s >> e;
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
queue <Node>q;
q.push({s, 0});
vis[s] = true;
int ans = -1;
while(!q.empty()) {
Node fr = q.front();
q.pop();
if(fr.x == e) {
ans = fr.step;
break;
}
int it = fr.x + a[fr.x];
if(it <= n && !vis[it]) {
vis[it] = true;
q.push({it, fr.step + 1});
}
it = fr.x - a[fr.x];
if(it <= n && !vis[it]) {
vis[it] = true;
q.push({it, fr.step + 1});
}
}
cout << ans;
}
这里空空如也
有帮助,赞一个