NOI '01 P3 - The Clever Typist

View as PDF

Submit solution

Points: 15 (partial)
Time limit: 1.0s
Memory limit: 64M

Problem type
National Olympiad in Informatics, China, 2001

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 describes 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 Specification

The input consists 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 Specification

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:

Key Digits after Press
123456
Swap1 623451
Right 623451
Swap0 263451
Down 253451
Right 253451
Up 254451
Right 254451
Down 254351
Right 254351
Up 254361
Swap0 654321

Therefore, the smallest number of keystrokes is 11.

Problem translated to English by Alex.


Comments

There are no comments at the moment.