【洛谷 1135】 奇怪的电梯

来源:互联网 发布:淘宝宝贝描述在哪里找 编辑:程序博客网 时间:2024/05/16 07:35

思路

广搜题,每次遇到满足条件的就加入队列。

代码

#include <bits/stdc++.h>using namespace std;struct node{    int now, step;};int n, a, b;int a1[210];bool vis[210];queue <node> q1;int main(){    scanf("%d%d%d", &n, &a, &b);    for(int i = 1; i <= n; i ++) scanf("%d", &a1[i]);    q1.push((node){a,0});    while(!q1.empty()){        node t = q1.front();        q1.pop();        if(t.now == b){            printf("%d", t.step);            return 0;        }        if(t.now - a1[t.now] >= 1 && !vis[t.now - a1[t.now]])             q1.push((node){t.now - a1[t.now], t.step+1}), vis[t.now - a1[t.now]] = 1;        if(t.now + a1[t.now] <= n && !vis[t.now +a1[t.now]])             q1.push((node){t.now + a1[t.now], t.step+1}), vis[t.now + a1[t.now]] = 1;    }    printf("-1");    return 0;}
0 0