bnuoj4221

键盘上的蚂蚁

1000ms
65536KB
 
64-bit integer IO format: %lld      Java class name: Main
Font Size:
Type:

  

键盘上有一只蚂蚁,而且是会打字的蚂蚁。当然,蚂蚁不能像我们人类一样用双手去敲击键盘。它只能够从一个按键爬到另外一个相邻的按键,然后用尽全身的力量原地跳下去按下按键。
考虑上图中的键盘,有公共边的按键被认为是相邻的,蚂蚁只能够在相邻的按键之间移动,而不能够跳跃。例如,它要打“ACM”这个单词的时候,他就会先移动到A上,按下A后,接着沿着A->Z->X->C的路径逐个键的移动,再到C,按下C,然后又移动到M上,按下M,接着重复移动敲击的过程,直到最后一个字符被输出。蚂蚁的这种打字方式要花费很多时间,所以它们期望做最少的移动,就能够完成特定的文本输入。
现在请你写一个程序计算一下在给定一段特定的文本的情况下,蚂蚁需要经过多少步移动,例如从QS的移动Q->W->S被记做2步移动。
注意:在移动过程中,蚂蚁当且仅当需要按空格的时候才能够移动到空格上(即在除空格外的按键间移动的时候是不允许经过空格的,比如从输入“CM”,是不能走C->Space->M这样的路径的,而要输入“C M”的时候就可以走C->Space->M这样的路径)。假设蚂蚁开始时恰好在文本第一个字符对应的按键上面。

Input

 输入数据只有一行,包括一行文本,文本中的字符保证能够使用上面键盘图中的按键输入(不必考虑大小写和上档键的使用,认为蚂蚁只要能够移动到按键上就能够打出按键上的任何字符),文本长度不超过1400个字符。

 

Output

输出数据包含一个整数,给出了蚂蚁敲击上面一段文本所需要的最少移动次数(从第一个按键按下到最后一个按键被按下为止) 

 

Sample Input

ACM
 

Sample Output

7
 

Source

Author

huangkun @ BNU
 
 
 
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <algorithm>
#include <queue>
using namespace std;
struct Nod
{
    int x,y,step;
} point1,point2;
int step[500][500];
char temp[500];
int dir[6][2]= {0,1,-1,0,0,-1,1,0,1,-1,-1,1};
char str[][30]={
    "`1234567890-=*",
    "*QWERTYUIOP[]\\",
    "*ASDFGHJKL;'**",
    "*ZXCVBNM,./***",
    "*** ******",
};
int mark[10][30];
void BFS(int x,int y,int nx,int ny)
{
    memset(mark,0,sizeof(mark));
    point1.x = x;
    point1.y = y;
    point1.step = 0;
    char ch = str[x][y];
    step[ch][ch] = 0;
    if(temp[ch]!=0)
    {
        step[temp[ch]][temp[ch]]=0;
        step[ch][temp[ch]]=0;
        step[temp[ch]][ch]=0;
    }
    mark[x][y]=1;
    queue<Nod> Q;
    Q.push(point1);
    while(!Q.empty())
    {
        point2 = Q.front();
        Q.pop();
        int i;
        for(i=0; i<6; i++)
        {
            point1.x = point2.x + dir[i][0];
            point1.y = point2.y + dir[i][1];
            point1.step = point2.step + 1;
            if(point1.x>=0&&point1.x<nx&&point1.y>=0&&point1.y<ny&&str[point1.x][point1.y]!='*'&&!mark[point1.x][point1.y])
            {
                mark[point1.x][point1.y]=1;
                step[str[point1.x][point1.y]][ch]=point1.step;
                if(temp[str[point1.x][point1.y]]!=0)
                {
                    step[temp[str[point1.x][point1.y]]][ch]=point1.step;
                }
                if(temp[ch]!=0)
                {
                    step[str[point1.x][point1.y]][temp[ch]]=point1.step;
                }
                if(temp[str[point1.x][point1.y]]!=0&&temp[ch]!=0)
                {
                    step[temp[str[point1.x][point1.y]]][temp[ch]]=point1.step;
                }
                Q.push(point1);
            }
        }
    }
}
void getStepArr()
{
    memset(step,-1,sizeof(step));
    char cc[]="CVBNM,";
    int i,j,k;
    for(i=0; i<4; i++)
    {
        for(j=0; j<14; j++)
        {
            char ch = str[i][j];
            if(ch!='*')
            {
                BFS(i,j,4,15);
            }
        }
    }
    for(i=0; i<4; i++)
    {
        for(j=0; j<14; j++)
        {
            char ch = str[i][j];
            if(ch!='*')
            {
                int mins = 99999999;
                for(k=0; k<6; k++)
                {
                    if(mins>step[ch][cc[k]])
                    {
                        mins=step[ch][cc[k]];
                    }
                }
                step[ch][' ']=mins+1;
                step[' '][ch]=mins+1;
                if(temp[ch]!=0)
                {
                    step[temp[ch]][' ']=mins+1;
                    step[' '][temp[ch]]=mins+1;
                }
            }
        }
    }
}
char q[2000];
int main()
{
    memset(temp,0,sizeof(temp));
    temp['`']='~';
    temp['1']='!';
    temp['2']='@';
    temp['3']='#';
    temp['4']='$';
    temp['5']='%';
    temp['6']='^';
    temp['7']='&';
    temp['8']='*';
    temp['9']='(';
    temp['0']=')';
    temp['-']='_';
    temp['=']='+';
    temp['[']='{';
    temp[']']='}';
    temp['\\']='|';
    temp[';']=':';
    temp['\'']='"';
    temp[',']='<';
    temp['.']='>';
    temp['/']='?';
    getStepArr();
    while(gets(q))
    {
        int len = strlen(q);
        int i,ans=0;
        for(i=0; i<len; i++)
            q[i]=toupper(q[i]);
        for(i=0; i<len-1; i++)
        {
            ans+=step[q[i]][q[i+1]];
        }
        printf("%d\n",ans);
    }
    return 0;
}
posted @ 2013-09-25 21:48  我家小破孩儿  阅读(335)  评论(0)    收藏  举报