“P1135 奇怪的电梯”的题解

· · 题解

#include <bits/stdc++.h>
#define PI_0 3
#define PI_1 3.1
#define PI_2 3.14
#define PI_3 3.142
#define PI_4 3.1416
#define PI_5 3.14159
#define PI_6 3.141593
#define PI_7 3.1415927
#define __UNSIGNED_TYPE_MIN__ 0
#define UNSIGNED_TYPE_MIN __UNSIGNED_TYPE_MIN__
using namespace std;
typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;

struct node
{
    int left_child_node, right_child_node;
    node(int __lcn = 0, int __rcn = 0)
    {
        left_child_node = __lcn;
        right_child_node = __rcn;
    }
    inline bool have_left_child_node()
    {
        return (left_child_node != 0);
    }
    inline bool have_right_child_node()
    {
        return (right_child_node != 0);
    }
    inline int child_num()
    {
        int res = 0;
        if(have_left_child_node())
        {
            res++;
        }
        if(have_right_child_node())
        {
            res++;
        }
        return res;
    }
}t[1];

inline ull sq(const int& x)
{
    return (ull)((ll)(x) * x);
}

inline float sq(const float& x)
{
    return x * x;
}

inline double sq(const double& x)
{
    return x * x;
}

inline bool is_prime(const int& x)
{
    if(x == 0 || x == 1)
    {
        return false;
    }
    for(int i = 2; i * i <= x; i++)
    {
        if(x % i == 0)
        {
            return false;
        }
    }
    return true;
}

inline bool is_odd_num(const int& x)
{
    return (x % 2);
}

inline bool is_even_num(const int& x)
{
    return (!(x % 2));
}

inline string itos(const int& x)
{
    return to_string(x);
}

inline ull fac(const int& x)
{
    ull ans = 1;
    if(x < 1)
    {
        return ans;
    }
    else if((x <= 2) && (x > 0))
    {
        return (ull)(x);
    }
    else
    {
        for(int i = x; i > 1; i--)
        {
            ans *= i;
        }
        return ans;
    }
}

inline int gcd(int x, int y)
{
    x = (x >= 0 ? x : -x);
    y = (y >= 0 ? y : -y);
    if((x == 1) || (y == 1))
    {
        return 1;
    }
    if(x == y)
    {
        return x;
    }
    while(y)
    {
        int t = x % y;
        x = y;
        y = t;
    }
    return x;
}

inline ll lcm(int x, int y)
{
    x = (x >= 0 ? x : -x);
    y = (y >= 0 ? y : -y);
    return (ll)(x) / gcd(x, y) * y;
}

inline int queue_find(queue<int> q, const int& x)
{
    for(int i = 0; !(q.empty()); i++)
    {
        if(q.front() == x)
        {
            return i;
        }
        else q.pop();
    }
    return -1;
}

inline ull qpow(const int& x, int y)
{
    int res = 1, base = x;
    while(y > 0)
    {
        if(y & 1)
        {
            res *= base;
        }
        base *= base;
        y /= 2;
    }
    return res;
}

inline ull P(int n, int m)
{
    return fac(n) / fac(n - m);
}

inline ull C(int n, int m)
{
    return P(n, m) / fac(m);
}

static char buf[1 << 14];

inline char getch()
{
    static char *begin_ = buf, *end_ = buf;
    begin_ = buf;
    end_ = buf + fread(buf, 1, 1 << 14, stdin);
    return *begin_++;
}

inline int input()
{
    bool sgn = false;
    int ret = 0, ch = getch();
    while(!(isdigit(ch)))
    {
        sgn |= (ch == '-');
        ch = getch();
    }
    while(isdigit(ch))
    {
        ret = ret * 10 + ch - '0';
        ch = getch();
    }
    return (sgn ? -ret : ret);
}

inline ui uiinput()
{
    ui u;
    scanf("%u", &u);
    return u;
}

inline ll llinput()
{
    ll g;
    scanf("%lld", &g);
    return g;
}

inline ull ullinput()
{
    ull u;
    scanf("%llu", &u);
    return u;
}

inline char charinput()
{
    return getch();
}

inline bool boolinput()
{
    bool b = false;
    int n = 0;
    scanf("%1d", &n);
    if(n == 1)
    {
        b = true;
    }
    return b;
}

inline float floinput()
{
    float f;
    scanf("%f", &f);
    return f;
}

inline double douinput()
{
    double d;
    scanf("%lf", &d);
    return d;
}

inline string strinput()
{
    string str = "";
    cin >> str;
    return str;
}

inline int output(int x = 0)
{
    if(x < 0)
    {
        putchar_unlocked('-'), x = -x;
    }
    if(x > 9)
    {
        output(x / 10);
    }
    putchar_unlocked(x % 10 + '0');
    return x;
}

inline int output_(const int& x = 0)
{
    output(x);
    putchar_unlocked(' ');
    return x;
}

inline int outputnl(const int& x = 0)
{
    output(x);
    putchar_unlocked('\n');
    return x;
}

inline ui uioutput_(const ui& u = 0)
{
    printf("%u ", u);
    return u;
}

inline ui uioutputnl(const ui& u = 0)
{
    printf("%u\n", u);
    return u;
}

inline ll lloutput_(const ll& g = 0)
{
    printf("%lld ", g);
    return g;
}

inline ll lloutputnl(const ll& g = 0)
{
    printf("%lld\n", g);
    return g;
}

inline ull ulloutput_(const ull& u = 0)
{
    printf("%llu ", u);
    return u;
}

inline ull ulloutputnl(const ull& u = 0)
{
    printf("%llu\n", u);
    return u;
}

inline char charoutput(const char& c = ' ')
{
    putchar_unlocked(c);
    return c;
}

inline char charoutput_(const char& c = ' ')
{
    charoutput(c);
    putchar_unlocked(' ');
    return c;
}

inline char charoutputnl(const char& c = ' ')
{
    charoutput(c);
    putchar_unlocked('\n');
    return c;
}

inline string stroutput(const string& str = "")
{
    int len = str.length();
    for(int i = 0; i < len; i++)
    {
        putchar_unlocked(str[i]);
    }
    return str;
}

inline string stroutput_(const string& str = "")
{
    stroutput(str);
    putchar_unlocked(' ');
    return str;
}

inline string stroutputnl(const string& str = "")
{
    stroutput(str);
    putchar_unlocked('\n');
    return str;
}

inline void backspace()
{
    charoutput('\b');
    return;
}

inline int max_3(const int& x, const int& y, const int& z)
{
    int maxp = x;
    if(y > maxp)
    {
        maxp = y;
    }
    if(z > maxp)
    {
        maxp = z;
    }
    return maxp;
}

inline int min_3(const int& x, const int& y, const int& z)
{
    int minp = x;
    if(y < minp)
    {
        minp = y;
    }
    if(z < minp)
    {
        minp = z;
    }
    return minp;
}

inline constexpr int arr_find_max(int* a, const int& x, const int& y)
{
    int res = a[x];
    for(int i = x + 1; i <= y; i++)
    {
        if(a[i] > res)
        {
            res = a[i];
        }
    }
    return res;
}

inline constexpr int arr_find_min(int* a, const int& x, const int& y)
{
    int res = a[x];
    for(int i = x + 1; i <= y; i++)
    {
        if(a[i] < res)
        {
            res = a[i];
        }
    }
    return res;
}

inline bool is_letter(const char& c)
{
    return ((c >= 'A') && (c <= 'z'));
}

inline void pre_order(const int& x)
{
    node tx = t[x];
    output_(x);
    if(tx.have_left_child_node())
    {
        pre_order(tx.left_child_node);
    }
    if(tx.have_right_child_node())
    {
        pre_order(tx.right_child_node);
    }
    return;
}

inline void in_order(const int& x)
{
    node tx = t[x];
    if(tx.have_left_child_node())
    {
        in_order(tx.left_child_node);
    }
    output_(x);
    if(tx.have_right_child_node())
    {
        in_order(tx.right_child_node);
    }
    return;
}

inline void post_order(const int& x)
{
    node tx = t[x];
    if(tx.have_left_child_node())
    {
        post_order(tx.left_child_node);
    }
    if(tx.have_right_child_node())
    {
        post_order(tx.right_child_node);
    }
    output_(x);
    return;
}

inline bool special_int_cmp(const int& x, const int& y)
{
    return (x > y);
}

inline bool special_ui_cmp(const ui& x, const ui& y)
{
    return (x > y);
}

inline bool special_ll_cmp(const ll& x, const ui& y)
{
    return (x > y);
}

inline bool special_ull_cmp(const ull& x, const ull& y)
{
    return (x > y);
}

inline void qsort(int* a, const int& l, const int& r)
{
    int i = l, j = r, flag = a[(l + r) / 2], t = 0;
    do
    {
        while(a[i] < flag)
        {
            i++;
        }
        while(a[j] > flag)
        {
            j--;
        }
        if(i <= j)
        {
            t = a[i];
            a[i] = a[j];
            a[j] = t;
            i++;
            j--;
        }
    }while(i <= j);
    if(l < j)
    {
        qsort(a, l, j);
    }
    if(i < r)
    {
        qsort(a, i, r);
    }
    return;
}

inline string encryption(const string& the_original, const int& secret_key)
{
    string ciphertext = the_original;
    int len = ciphertext.length();
    for(int i = 0; i < len; i++)
    {
        ciphertext[i] += secret_key;
    }
    return ciphertext;
}

inline string decryption(const string& ciphertext, const int& secret_key)
{
    string the_original = ciphertext;
    int len = the_original.length();
    for(int i = 0; i < len; i++)
    {
        the_original[i] -= secret_key;
    }
    return the_original;
}

struct _node
{
    int the_floor, step;
    _node(int _f = 0, int _s = 0)
    {
        the_floor = _f;
        step = _s;
    }
};

int n, a, b;
queue<_node> q;
_node now;

int main(void)
{
    scanf("%d %d %d", &n, &a, &b);
    vector<int> k(n + 1, 0);
    vector<bool> vis(n + 1, false);
    for(int i = 1; i <= n; i++)
    {
        scanf("%d", &k[i]);
    }
    q.push(_node(a, 0));
    vis[a] = true;
    while(!(q.empty()))
    {
        now = q.front();
        q.pop();
        if(now.the_floor == b)
        {
            outputnl(now.step);
            return UNSIGNED_TYPE_MIN;
        }
        else
        {
            if(now.the_floor + k[now.the_floor] <= n)
            {
                if(vis[now.the_floor + k[now.the_floor]] == false)
                {
                    q.push(_node(now.the_floor + k[now.the_floor], now.step + 1));
                    vis[now.the_floor + k[now.the_floor]] = true;
                }
            }
            if(now.the_floor - k[now.the_floor] >= 1)
            {
                if(vis[now.the_floor - k[now.the_floor]] == false)
                {
                    q.push(_node(now.the_floor - k[now.the_floor], now.step + 1));
                    vis[now.the_floor - k[now.the_floor]] = true;
                }
            }
        }
    }
    outputnl(-1);
    return UNSIGNED_TYPE_MIN;
}