Maze Escape
Time limit2sMemory limit1024 MB
Given N rooms in a line, find the fewest teleports to room 1 or room N on a fastest walk from room S to room E.
- Level
Medium6 of 10
- Topics
- Math, Greedy, Implementation
- Solved
- No attempts yet
Problem
Juwon arrived at a maze while playing an escape room. The maze consists of rooms in a row, and room is adjacent to room (). Each room has one switch. Juwon starts in room , the entrance, presses every switch, and then moves to room , the exit, to escape.
Juwon can use two kinds of moves. The first costs and moves to an adjacent room. The second costs and teleports to room or room . Both kinds of moves take 1 unit of time each. Pressing a switch takes 0 time.
Escaping requires good time management, so Juwon wants the fastest way out of the maze. Among the ways that escape in the minimum time, find the minimum cost.
Input
The first line contains the number of test cases . Each of the following lines gives , , and for one test case, separated by spaces.
Output
For each test case, print the minimum possible cost on one line.
Constraints
- All numbers in the input are integers.