Dual Channel VHF Radio

Interview

Time limit1sMemory limit512 MB

Summary
Given two stored frequencies, the active channel, and a target, find the fewest button presses to receive the target using direct entry, UP steps, or DOWN steps, plus channel switching.
Level

Medium5 of 10

Topics
Math, Implementation, Simulation
Solved
No attempts yet

Problem

There is a 2-channel radio with channel A and channel B. The supported frequency range is from 144.000 MHz to 146.000 MHz inclusive. The frequency cannot be adjusted in units smaller than 0.001 MHz. You can set the frequency of each channel, but radio reception is possible on only one channel, the currently set channel.

For example, if channel A is set to 145.000 MHz and channel B to 145.500 MHz, and the current channel is A, then 145.000 MHz is received. If you switch the channel to B in this state, 145.500 MHz is received. The frequency can be changed only on the current channel. To change the frequency of the channel that is not currently set, you must first switch the channel and then change the frequency.

The available buttons fall into three groups.

  • Pressing the A/B button switches the current channel from A to B, or from B to A.

  • You can enter the frequency of the current channel directly with the numeric keypad from 0 to 9. Enter all 6 digits of the desired frequency in order. All 6 digits must be entered at once. For example, to enter 144.678 MHz, press 1 4 4 6 7 8 in order.

  • You can adjust the frequency of the current channel in steps of 0.02 MHz with the UP and DOWN buttons. The step size is fixed at 0.02 MHz. For example, pressing UP once at 145.005 MHz gives 145.025 MHz.

If pressing the UP or DOWN button would go outside the supported frequency range, the frequency is forced to the opposite boundary.

  • Pressing UP once at 146.000 MHz sets the frequency to 144.000 MHz.
  • Pressing UP once at 145.999 MHz sets the frequency to 144.000 MHz.
  • Pressing DOWN once at 144.000 MHz sets the frequency to 146.000 MHz.
  • Pressing DOWN once at 144.010 MHz sets the frequency to 146.000 MHz.

Frequency adjustment is done using one of three strategies.

  1. A strategy using only the A/B button and the numeric keypad
  2. A strategy using only the A/B button and the UP button
  3. A strategy using only the A/B button and the DOWN button

In the process of changing the frequency, the sequence of button presses is not unique. For example, if you want to change the frequency of the current channel from 145.000 MHz to 145.040 MHz,

  • Following strategy 1, use the numeric buttons. Press 1 4 5 0 4 0 in order.
  • Following strategy 2, use the UP button. Press the UP button twice.
  • There are countless other ways as well. You can keep changing the frequency as much as you like and stop at 145.040 MHz at the end.

Comparing the number of button presses, strategy 1 takes 6 presses and strategy 2 takes 2 presses. Here strategy 2 is more efficient.

The input gives the frequency set on channel A, the frequency set on channel B, the currently set channel, and the target frequency. Write a program that computes the minimum number of button presses needed so that the target frequency is received. The channel used to receive the target frequency may be either channel A or channel B.

Input

The first line gives the number of test cases. The number of test cases is at most 5,000. From the second line, the test cases are given one per line. The format of each test case is as follows.

The frequency set on channel A, the frequency set on channel B, the current channel, and the target frequency are given, separated by spaces. The current channel is given as the uppercase letter A or B. Every input frequency value is between 144 MHz and 146 MHz inclusive and is always expressed to three decimal places. The unit of the frequency is omitted.

Output

For each test case, output the minimum number of button presses needed so that the target frequency is received, one per line. If the target frequency is already set on the current channel in the given situation, the minimum number of button presses is 0.

Examples1

  1. Example 1

    Input
    4
    145.000 146.000 A 145.000
    145.000 146.000 B 145.000
    145.500 145.600 B 145.480
    145.000 144.020 A 146.000
    
    Expected output
    0
    1
    2
    3