Mines Motor Company

시간 제한1초메모리 제한2048 MB

요약
행과 열을 알파벳으로 표시한 격자에서 방문할 작업장 순서가 주어질 때, 연속한 작업장 사이의 맨해튼 거리 합을 구한다.
난이도

쉬움10점 중 2점

유형
수학, 구현
정답자
아직 제출이 없습니다

문제

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 11 shows this grid. Travel between two workstations can only occur via the horizontal and vertical paths, and each workstation is exactly 11 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 22.

Figure 1: The production plant grid layout.

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?

입력

The first line of input is ≤N≤100,000 \leq N \leq 100\\,000, the number of workstations that have to be visited. The remaining NN 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 an integer representing the total distance traveled to complete that path.

예제5

  1. 예제 1

    입력
    2
    AA
    CC
    
    예상 출력
    4
    
  2. 예제 2

    입력
    3
    AA
    AB
    AC
    
    예상 출력
    2
    
  3. 예제 3

    입력
    7
    HE
    SB
    RI
    YV
    NR
    IG
    WN
    
    예상 출력
    94
    
  4. 예제 4

    입력
    11
    BR
    XW
    BT
    SN
    UM
    IL
    OP
    VT
    ZZ
    SU
    LR
    
    예상 출력
    144
    
  5. 예제 5

    입력
    5
    AA
    BB
    CC
    DD
    EE
    
    예상 출력
    8