TopCoder problem "PhonePad" used in SRM 244 (Division II Level Two)



Problem Statement

    A typical phone pad has the following layout:
 1 2 3
 4 5 6
 7 8 9
 * 0 #
The finger movement distance between any pair of keys is defined as their Manhattan distance (the sum of the absolute differences of their coordinates) on the phone pad. For example, the key '6' is 0 units away from itself, 1 unit away from '3', '5', and '9', 2 units away from '2', '4', '8', and '#', 3 units away from '1', '7', and '0', and 4 units away from '*'. Write a class PhonePad with a method fingerMovement that takes a String, phoneNumber, and returns the minimal distance of total finger movement distance required to dial the phone number. Note that the finger is initially on key '5'.
 

Definition

    
Class:PhonePad
Method:fingerMovement
Parameters:String
Returns:int
Method signature:int fingerMovement(String phoneNumber)
(be sure your method is public)
    
 

Constraints

-phoneNumber will be between 3 and 20 characters long, inclusive.
-phoneNumber will consist of only digits ('0'-'9').
 

Examples

0)
    
"911"
Returns: 6
From the initial position '5', the finger has to move 2 units to reach the key '9', then 4 units to the key '1' from there. Finally, we can enter the last digit '1' without moving the finger. The return value is thus 2 + 4 + 0 = 6.
1)
    
"5555555"
Returns: 0
We don't have to move the finger at all, so the result is 0.
2)
    
"8606335540"
Returns: 16
3)
    
"8606574276"
Returns: 21

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=4464

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=7219&pm=4464

Writer:

logged

Testers:

PabloGilberto , lbackstrom , brett1479

Problem categories:

Simple Search, Iteration, Simulation