Problem M
Mines Motor Company
A group of highly successful Mines Computer Science and Mechanical Engineering graduates decided to create an electric car company called Mines Motor Company. The company has been very successful and recently opened their production plant. In the production plant, there are multiple workstations in a grid layout. The rows and columns of the grid are both labeled using uppercase letters (A-Z for rows from top to bottom, and A-Z for columns from left to right). Figure $1$ shows this grid. Travel between two workstations can only occur via the horizontal and vertical paths, and each workstation is exactly $1$ unit away from its four cardinally-adjacent workstations (above, below, right, and left). For instance to go from AA to BB a product must first visit either AB or BA before proceeding on to BB, resulting in a distance of $2$.
In the current layout, the engineers noticed that products must travel a long distance through the plant to be completed. To optimize the location of the workstations, the company wants to track a single product’s travel within the plant. As a start for our tracking software, we need to program an algorithm that answers the following question:
Given the order of workstations that a product has to visit, what is the total distance traveled?
Input
The first line of input is $ \leq N \leq 100\, 000$, the number of workstations that have to be visited. The remaining $N$ lines are the locations of the workstations in the format [ROW][COLUMN] where [ROW] and [COLUMN] are both single uppercase letters indicating the row and column of the workstation. You can assume that the product is already at the first workstation and will stay at the last workstation.
Output
Output an integer representing the total distance traveled to complete that path.
Sample Input 1 | Sample Output 1 |
---|---|
2 AA CC |
4 |
Sample Input 2 | Sample Output 2 |
---|---|
3 AA AB AC |
2 |
Sample Input 3 | Sample Output 3 |
---|---|
7 HE SB RI YV NR IG WN |
94 |
Sample Input 4 | Sample Output 4 |
---|---|
11 BR XW BT SN UM IL OP VT ZZ SU LR |
144 |
Sample Input 5 | Sample Output 5 |
---|---|
5 AA BB CC DD EE |
8 |