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 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), fi nd 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 con guration of the elevator.


10 1 10 2 1

100 2 1 1 0 

use the stairs

hide comments
surajkumar_cse: 2018-12-14 11:45:57

I was getting TLE in my first attempt. Everything was correct.
then I got a slight hint that:
i was making that floor visited after popping it out of the queue.

So I corrected it and make visited as soon as I push that floor in to the queue. AC

I hope this mistake will save my time in further questions.

noobie32: 2018-12-11 21:24:04

AC in one go!!

sachinspoj: 2018-10-06 13:56:21

AC in one go!
Easy bfs :)

aryan12: 2018-09-04 17:28:19

A nice question for a beginner like me in bfs. It took me a lot of time to figure out the right answer and finally I did it. The problem which may occur in some of your codes can be a WA in 12th test case as mentioned by the previous comments.I will just leave a small hint to make you figure out what is wrong in your code.

HINT: All which start may not end.

Last edit: 2018-09-04 17:29:38
taponidhi: 2018-06-13 07:11:30

Those who are getting WA in 12th testcase,note that starting floor mustn't be below the final floor.Even final floor can be below the starting floor.So when you make the comparison of cur_floor+u or cur_floor-d,use top,bottom variable to store maximum of (start,end) & minimum of (start,end) and then make comparisons.

Last edit: 2018-06-13 07:12:20
akash13s: 2018-05-31 09:30:36

AC in 2nd go!
nice question on bfs:)

pithoriya: 2018-05-23 06:23:56

solved using AP ...and if ..else

spa1ish: 2018-05-02 14:58:20

solved using if..else logic...ac in one go

avik26091998: 2018-02-25 21:40:42

My first in BFS!!!

vivek_prime: 2018-01-14 19:20:25

ac in one go :)

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