ELEVTRBL - Elevator Trouble


You are on your way to your rst job interview as a program tester, and you are already
late. The interview is in a skyscraper and you are currently in
oor 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
oors, 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), 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.

Input

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].

Output

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 elvator.

Example

Input:
10 1 10 2 1
Output:
6

hide comments
thaihoc06: 2016-07-22 09:30:33

Simple BFS :) AC in one go!

komninosk: 2016-07-11 17:16:50

thanks for stating the obvious bois

starter007: 2016-05-19 12:20:28

I m getting TLE in C++ why?? i m using STL for Queue please HELP ADMIN

Last edit: 2016-05-19 12:21:03
Sachin Bisht : 2016-04-23 09:29:21

I am trying to implement this problem in JAVA but getting TLE.
Can anyone help me out. (FYI I am using ArrayDeque instead of LinkedList but still getting TLE)

g4mewarrior: 2016-03-16 21:42:21

simple bfs!!
do check your way of pushing into queue

try2catch: 2016-02-21 10:20:53

finally understood ! meaning of BFS.

nitin jain: 2016-01-28 21:05:05

simple BFS :)

darkhire21: 2016-01-17 18:40:49

graph is too big declare queue at top ...!!

Deepak : 2016-01-09 12:58:43

just bfs :)

gaurav_994: 2015-12-28 06:09:03

can i in any way find the testcases for this problem i m getting wrong answer cant find why


Added by:Krzysztof Lewko
Date:2011-10-06
Time limit:0.148s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All
Resource:Nordic programming contest