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
nadstratosfer: 2017-10-08 23:21:37

Don't bother solving this one with Python, setter not competent enough to set achievable limits.

lehuunghia: 2017-07-14 20:37:47

i have tested it carefully at my computer, look like it give me the correct result , but when i submit solution ID =19796863 i receive this this warning 'time limited exceeded' , then i have refactored code, Unluckily the issue still there. Could someone please give me a hand to check , how i can avoid this warning , Thanks

aabbccdd: 2017-07-09 16:19:27

@tusharuppal - why lie about it? You were accepted on your fifth attempt!

daredevil_31: 2017-06-15 04:12:27

@kunal todi, hi did u get why after running 12 test cases, it gives wrong answer?

tusharuppal: 2017-04-06 14:15:29

AC in one go..good qn to practice bfs.||

cake_is_a_lie: 2017-04-03 15:56:32

@flyingduchman_: <<In BFS, you should set "visited" array size to a big value like bool visited[10000000] to handle corner cases. >>

Or you COULD learn proper programming and set the size to exactly what the problem restriction says. Setting it 10 times bigger to handle the "corner cases" simply says you have no idea what the corner cases are so can't properly handle them. Therefore, you pray that if you grossly over-allocate the visited array this will somehow solve your problem - a wonderful programming technique also known as "shotgun debugging".

holmesherlock: 2017-03-30 00:45:00

thanx @danish4200

kshubham02: 2017-03-20 18:55:38

Tutorial Level :/

danish4200: 2017-01-30 17:01:28

dont forget the case when you are standing on the same floor where you have to reach..

tanny_123: 2017-01-28 13:38:09

could someone check my solution ID = 18657999

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