mle求调
查看原帖
mle求调
1235820
yatoors楼主2025/6/30 20:33
#include <bits/stdc++.h>
using namespace std;

int n, a, b;
int arr[1000];
bool vis[1000];

int main() {
	cin >> n >> a >> b;
	for (int i = 1; i <= n; i++) {
		cin >> arr[i];
	}
	queue<pair<int, int> > q;
	q.push({a, 0});
	while (!q.empty()) {
		pair<int, int> nw = q.front();
		vis[nw.first] = 1;
		if (nw.first == b) {
			cout << nw.second << endl;
			return 0;
		}
		q.pop();
		int t1 = nw.first - arr[nw.first], t2 = nw.first + arr[nw.first];
		if (t1 >= 1 && vis[t1] == 0) q.push({t1, nw.second + 1});
		if (t2 <= n && vis[t2] == 0) q.push({t2, nw.second + 1});
	}
	cout << -1 << endl;
	return 0;
} 
2025/6/30 20:33
加载中...