ELEVTRBL - Elevator Trouble

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 elevator, 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-button 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 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, g <= f <= 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 conguration of the elevator.


10 1 10 2 1

100 2 1 1 0 

use the stairs

hide comments
manish_thakur: 2020-05-25 20:24:08

@aabbccdd savage! :D

unicorn_98: 2020-04-25 07:49:56

AC in 1 go !!!!!

raju_singho: 2020-04-18 19:05:28

can anyone tell me how to pass test case 11???...I got WA
Now i got wa at tc 12...:(

Last edit: 2020-04-18 19:27:10
nikita_omare98: 2020-04-15 11:54:41

bfs is amazaing

coolio_1: 2020-03-09 18:45:32

AC in 1 go!

majd211: 2020-02-22 01:52:42

easy bfs :)

prateekpandey: 2020-01-22 06:05:44

sometimes you have to do nothing :) :)s=f;

ak8325: 2020-01-03 02:41:55

See you can always go from same floor to same floor.. don't ignore it

hitesh87: 2019-11-30 17:55:46

nice problem beware of tc11

manas_uchiha10: 2019-08-18 09:13:34

Test case 11 gave me RE ..nice one

Added by:Krzysztof Lewko
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:Nordic programming contest