|
- #include <cstdio>
- #include <queue>
- const int N = 210;
- int n, b, a, k[N];
- bool vis[N];
- struct Sta {
- int floor, step;
- Sta(int f, int s) {
- this->floor = f;
- this->step = s;
- }
- };
- void BFS() {
- std::queue<Sta> Q;
- Q.push(Sta(a, 0));
- vis[a] = 1;
- while(!Q.empty()) {
- Sta s = Q.front();
- Q.pop();
- if(s.floor == b) {
- printf("%d", s.step);
- return;
- }
- int p = s.floor + k[s.floor];
- if(p > 0 && p <= n && !vis[p]) {
- vis[p] = 1;
- Q.push(Sta(p, s.step + 1));
- }
- p = s.floor - k[s.floor];
- if(p > 0 && p <= n && !vis[p]) {
- vis[p] = 1;
- Q.push(Sta(p, s.step + 1));
- }
- }
- printf("-1");
- return;
- }
- int main() {
- scanf("%d%d%d", &n, &a, &b);
- for(int i = 1; i <= n; i++) {
- scanf("%d", &k[i]);
- }
- BFS();
- return 0;
- }
复制代码 |
|