National Olympiad in Informatics, China, 2001

Day 1, Problem 3 - The Clever Typist

Alana is a typist for a secret organization. She has accepted a task: input the data for several hundred numerical passwords of a fixed length 6. Of course, she wishes to hit as few keys on the keyboard as possible during the process.

Unfortunately due to the new demand for secrecy, the organization's keyboard for password input is specially designed. There are no numerical keys on the keyboard, instead only the six keys: Swap0, Swap1, Up, Down, Left, and Right. To explain the functions of these 6 keys, we assign indices to the 6 fields where digit are inserted. These fields, from left to right, are labeled 1, 2, 3, 4, 5, and 6 respectively. The following describe the function of each key:

  • Swap0: When pressed, the cursor's position does not change. The digit at the cursor's position is swapped with the digit at index 1 (the leftmost field). If the digit is already at index 1, then pressing Swap0 will do nothing.
  • Swap1: When pressed, the cursor's position does not change. The digit at the cursor's position is swapped with the digit at index 6 (the rightmost field). If the digit is already at index 6, then pressing Swap1 will do nothing.
  • Up: When pressed, the cursor's position does not change. The digit at the cursor's position is incremented by 1 (unless the digit is 9). E.g. if the digit at the cursor's position is 2, then it will become 3 after pressing Up. If the digit was 9, then neither the digit nor the cursor's position would change after pressing Up.
  • Down: When pressed, the cursor's position does not change. The digit at the cursor's position is decremented by 1 (unless the digit is 0). If the digit is 0, then when Down is pressed, neither the current digit nor the cursor's position would change.
  • Left: When pressed, the cursor's current position is moved left by one position. If the index of the cursor is 1 before pressing the key (the first position from the left), then the cursor does not move.
  • Right: When pressed, the cursor's current position is moved right by one position. If the index of the cursor is 6 before pressing the key (the last position from the left), then the cursor does not move.

Of course, for the special keyboard to be effective at its job, the input fields will be instantly filled with an initial password of length 6 just before each password is inputted. The cursor's position will initially appear at index 1 each time. When the 6 special keys as described above are used properly on the initial passwords, one will be able to produce the target password to be inputted. The cursor is allowed to terminate at any position. Alana needs your help. Write a program that finds the minimum number of keystrokes required to input a given password.

Input Format

The input consist of one line, containing two space-separated 6 digit numbers. The first number is the initial password, and the second number is the target password.

Output Format

The output should contain one positive integer, the minimum number of keystrokes required.

Sample Input

123456 654321

Sample Output

11

Explanation

The initial password is 123456. The cursor is initially stopped at index 1. The smallest sequence of keystrokes corresponding to the sample output is:

KeyDigits after Press
123456
Swap1623451
Right623451
Swap0263451
Down253451
Right253451
Up254451
Right254451
Down254351
Right254351
Up254361
Swap0654321

Therefore, the smallest number of keystrokes is 11.

All Submissions
Best Solutions


Point Value: 15 (partial)
Time Limit: 2.00s
Memory Limit: 64M
Added: May 08, 2014

Languages Allowed:
C++03, PAS, C, HASK, ASM, RUBY, PYTH2, JAVA, PHP, SCM, CAML, PERL, C#, C++11, PYTH3

Comments (Search)

It's quiet in here...