妈妈的马达15

RRRRRRrrrrrrr 2023-11-25 9:44:05

#include <bits/stdc++.h>
using namespace std;
#define int long long

int n,a[100005];
int dp[100005];

void ddd() {
	queue<int> q;
	q.push(n);
	dp[n] = 0;
	while(!q.empty()){
		int x = q.front();
		q.pop();
		if (x-a[x] >= 1 && dp[x-a[x]] == -1) {
			q.push(x-a[x]);
			dp[x-a[x]] = dp[x]+1;
		}
		if (x+a[x] <= n && dp[x+a[x]] == -1) {
			q.push(x+a[x]);
			dp[x+a[x]] = dp[x]+1;
		}
	}
}

signed main() {
	std::ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		dp[i] = -1;
	}
	ddd();
	cout << dp[1];
	return 0;
}