Problem Name: beecrowd | 3250

Problem Link:

Elevator Trouble


By Pål Grønås Drange NO Norway

Timelimit: 1

You are on your way to your first job interview as a program tester, and you are already late. The interview is in a skyscraper and you are currently in floor s, where you see an elevator. Upon entering the elvator, you learn that it has only two buttons, marked “UP u” and “DOWN d”. You conclude that the UP-button takes the elevator u floors up (if there aren’t enough floors, pressing the UP-botton does nothing, or at least so you assume), whereas the DOWN-button takes you d stories down (or none if there aren’t enough). Knowing that the interview is at floor g, and that there are only f floors in the building, you quickly decide to write a program that gives you the amount of button pushes you need to perform. If you simply cannot reach the correct floor, your program halts with the message “use the stairs”.

Given input f, s, g, u and d (floors, start, goal, up, down), find the shortest sequence of button presses you must press in order to get from s to g, given a building of f floors, or output “use the stairs” if you cannot get from s to g by the given elevator.




The input will consist of one line, namely f s g u d, where 1 ≤ s, gf ≤ 1000000 and 0 ≤ u, d ≤ 1000000. The floors are one-indexed, i.e. if there are 10 stories, s and g be in [1, 10].




You must reply with the minimum numbers of pushes you must make in order to get from s to g, or output "use the stairs" if it is impossible given the configuration of the elevator.




Input Samples Output Samples

10 1 10 2 1





100 2 1 1 0

use the stairs


Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <bits/stdc++.h>

using namespace std;

vector < vector<int>> grafo(1000001);

bool bfs(int s, int n, int meta, vector<int> &dist, vector<int> &pred){
    vector < bool> visited(n, false);

    list<int> queue;

    visited[s] = true;
    dist[s] = 0;

        s = queue.front();

        for(int i = 0; i  <  grafo[s].size(); ++i){
                visited[grafo[s][i]] = true;
                dist[grafo[s][i]] = dist[s] + 1;
                pred[grafo[s][i]] = s;

                //Há um caminho entre o andar inicial e o objetivo.
                if(grafo[s][i] == meta)
                    return true;

    return false;

void solve(int s, int dest, int v){
    vector<int> pred(v, -1), dist(v, INT_MAX);
    if(!bfs(s, v, dest, dist, pred)) {
        cout << "use the stairs\n";

    // Distância do andar de inicio e o objetivo.
    cout << dist[dest] << '\n';

int main(){

    int andares, inicio, meta, cima, baixo;

    cin >> andares >> inicio >> meta >> cima >> baixo;

    for(int i = 1; i <= andares; ++i){
        if(i + cima  < = andares){
            grafo[i].push_back(i + cima>;

        if(i - baixo >= 1){
            grafo[i].push_back(i - baixo);

    if(inicio == meta)
        cout << 0 << '\n';
        solve(inicio, meta, andares+1);

    return 0;

Copy The Code & Try With Live Editor


10 1 10 2 1




#3241 Beecrowd Online Judge Solution 3241 Help a PhD Candidate Out! Solution in C, C++, Java, Js and Python
#3258 Beecrowd Online Judge Solution 3258 Boiling Vegetables Solution in C, C++, Java, Js and Python