#P9856. [CCC 2008 J3] GPS Text Entry

[CCC 2008 J3] GPS Text Entry

题目描述

For her birthday, Sandy got a Global Positioning System (GPS) unit, which is an electronic device she can use to track the local hiking trails. Along the way Sandy can mark waypoints that can be recorded on a map when she gets home. A description of each waypoint can be entered in the unit, however the device does not have a keypad. Instead it has four cursor buttons, up, down, left, and right, and a button to accept the letter. The keypad looks like the following:

The screen displays a grid of the letters and symbols that can be used to “type out” the description. Here is the layout of the grid:

When you enter the name of the waypoint, the cursor starts at the A. You must move the cursor to the location of the next letter or symbol and then accept that letter. The cursor can only move to squares which are adjacent horizontally or vertically (not diagonally). Once you have entered all the letters in the description, you need to move the cursor to enter and accept the entire phrase.

You are to write a program that will calculate the number of cursor movements it takes to “type in” a phrase. For example, to enter the word GPS, starting from the A position, you would move down 11 to select G, then move right 33 and down 11 to select P, then move down 1 and left 33 to select S and finally move down 11 and right 55 to select enter. This is a total of 1515 cursor movements. Note that the total number of cursor movements does not change if you choose to move down and then across or across and then down. Also note that you cannot move beyond the boundaries of the grid (e.g., you cannot move off the grid nor wrap-around the grid).

输入格式

The input for your program will be a string of at most 4040 characters. You may assume that all characters in the string are contained in the grid

输出格式

The output for your program will be an integer that is the total number of cursor movements needed to enter the string using the grid layout given.

题目大意

给你一个字符串,其长度最多为 4040,你需要通过键盘打出这个字符串,你的打字工具目前使用的键盘如下:

初始时你在 A 处,你需要将此字符串从左往右依次打出。每次移动,你可以向上下左右任一方向移动一格,注意你不能从某个地方“瞬移”到另一个地方,只能通过上述方式移动,且当你输入完所有字符串时要按下 enter 键。

GPS
15
ECHO ROCK
29

提示

数据保证合法。