感谢 @[yummy](https://www.luogu.com.cn/user/101694) ) 提供的一些数据。
呵呵,有一天我做了一个梦,梦见了一种很奇怪的电梯。大楼的每一层楼都可以停电梯,而且第 $i$ 层楼($1 \le i \le N$)上有一个数字 $K_i$($0 \le K_i \le N$)。电梯只有四个按钮:开,关,上,下。上下的层数等于当前楼层上的那个数字。当然,如果不能满足要求,相应的按钮就会失灵。例如: $3, 3, 1, 2, 5$ 代表了 $K_i$($K_1=3$,$K_2=3$,……),从 $1$ 楼开始。在 $1$ 楼,按“上”可以到 $4$ 楼,按“下”是不起作用的,因为没有 $-2$ 楼。那么,从 $A$ 楼到 $B$ 楼至少要按几次按钮呢?
共二行。
第一行为三个用空格隔开的正整数,表示 $N, A, B$($1 \le N \le 200$,$1 \le A, B \le N$)。
第二行为 $N$ 个用空格隔开的非负整数,表示 $K_i$。
一行,即最少按键次数,若无法到达,则输出 -1。
5 1 5 3 3 1 2 5
3
对于 $100 \%$ 的数据,$1 \le N \le 200$,$1 \le A, B \le N$,$0 \le K_i \le N$。
本题共 $16$ 个测试点,前 $15$ 个每个测试点 $6$ 分,最后一个测试点 $10$ 分。
#include <bits/stdc++.h> #define AC return 0 using namespace std; struct node { int fl, d; } now; queue<node> q;//I prefer to use STL in the end. int n, a, b, k[300], bj[300]; void bfs() { while (!q.empty()) { now = q.front(); q.pop(); if (now.fl == b) break; for (int i = -1; i <= 1; i += 2) { int t = now.fl + k[now.fl] * i; if (t >= 1 && t <= n && bj[t] == 0) { q.push((node) { t, now.d + 1 }); bj[t] = 1; } } } } int main() { //freopen("02.in", "r", stdin); cin >> n >> a >> b; for (int i = 1; i <= n; i++) cin >> k[i]; q.push((node) { a, 0 }), bj[a] = 1; bfs(); if (now.fl == b) cout << now.d; else cout << -1; AC; }