Elevator
InterviewTime limit1sMemory limit256 MB
An elevator with buttons that move up U or down D floors, staying within floors 1 to F, needs the minimum presses to go from floor S to floor G.
- Level
Medium4 of 10
- Topics
- BFS, Graph, Shortest path, Implementation
- Solved
- No attempts yet
Problem
Kangho applied to a coding-education startup, and today is his interview day. But he overslept and arrived late at the building where the company is located.
The company occupies a tall building with floors in total, and its office is on floor . Kangho is currently on floor and wants to take the elevator to floor .
A normal elevator has a button for every floor, but the elevator Kangho boarded has only two buttons.
- Button U: go up by floors.
- Button D: go down by floors.
If going up by floors would rise above the top floor (), or going down by floors would drop below floor , the elevator does not move.
Find the minimum number of button presses Kangho needs to reach floor . If he cannot reach floor using only the elevator, print use the stairs.
Input
The first line contains five integers , , , , and , separated by spaces. (, )
The building starts at floor , and the highest floor is .
Output
Print the minimum number of button presses needed to go from floor to floor . If the elevator cannot reach floor , print use the stairs.