http://www.bnuoj.com/bnuoj/contest_show.php?cid=2876#problem/31796
【题意】:
如题,注意大小写情况
【code】:
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <ctype.h> 5 #include <algorithm> 6 #include <queue> 7 8 using namespace std; 9 10 struct Nod 11 { 12 int x,y,step; 13 }nd1,nd2; 14 15 int step[500][500]; 16 char temp[500]; 17 int cx[]={0,-1,0,1,1,-1}; //定义六个方向可以移动 18 int cy[]={1,0,-1,0,-1,1}; 19 char str[][30]={ 20 "`1234567890-=***", 21 "*QWERTYUIOP[]\\**", //这里的反斜杠‘\’必须打两个进行转义 22 "*ASDFGHJKL;'****", 23 "*ZXCVBNM,./*****", 24 "*** ********", 25 }; 26 27 int mark[10][30]; 28 29 void BFS(int x,int y,int nx,int ny) //4 14 / 5 14 30 { 31 memset(mark,0,sizeof(mark)); 32 nd1.x = x; 33 nd1.y = y; 34 nd1.step = 0; 35 char ch = str[x][y]; 36 step[ch][ch] = 0; 37 if(temp[ch]!=0) 38 { 39 step[temp[ch]][temp[ch]]=0; 40 step[ch][temp[ch]]=0; 41 step[temp[ch]][ch]=0; 42 } 43 mark[x][y]=1; 44 queue<Nod> Q; 45 Q.push(nd1); 46 while(!Q.empty()) 47 { 48 nd2 = Q.front(); 49 Q.pop(); 50 int i; 51 for(i=0;i<6;i++) 52 { 53 nd1.x = nd2.x + cx[i]; 54 nd1.y = nd2.y + cy[i]; 55 nd1.step = nd2.step + 1; 56 if(nd1.x>=0&&nd1.x<nx&&nd1.y>=0&&nd1.y<ny&&str[nd1.x][nd1.y]!='*'&&!mark[nd1.x][nd1.y]) 57 { 58 mark[nd1.x][nd1.y]=1; 59 step[str[nd1.x][nd1.y]][ch]=nd1.step; 60 if(temp[str[nd1.x][nd1.y]]!=0) 61 { 62 step[temp[str[nd1.x][nd1.y]]][ch]=nd1.step; 63 } 64 if(temp[ch]!=0) 65 { 66 step[str[nd1.x][nd1.y]][temp[ch]]=nd1.step; 67 } 68 if(temp[str[nd1.x][nd1.y]]!=0&&temp[ch]!=0) 69 { 70 step[temp[str[nd1.x][nd1.y]]][temp[ch]]=nd1.step; 71 } 72 Q.push(nd1); 73 } 74 } 75 } 76 } 77 78 void getStepArr() 79 { 80 memset(step,-1,sizeof(step)); 81 char cc[]="CVBNM,"; 82 int i,j,k; 83 for(i=0;i<4;i++) 84 { 85 for(j=0;j<15;j++) 86 { 87 char ch = str[i][j]; 88 if(ch!='*') 89 { 90 BFS(i,j,4,15); 91 } 92 } 93 } 94 //对空格键单独处理 95 for(i=0;i<4;i++) 96 { 97 for(j=0;j<14;j++) 98 { 99 char ch = str[i][j]; 100 if(ch!='*') 101 { 102 int mins = 10000; 103 for(k=0;k<6;k++) 104 { 105 if(mins>step[ch][cc[k]]) 106 { 107 mins=step[ch][cc[k]]; 108 } 109 } 110 step[ch][' ']=mins+1; 111 step[' '][ch]=mins+1; 112 if(temp[ch]!=0) 113 { 114 step[temp[ch]][' ']=mins+1; 115 step[' '][temp[ch]]=mins+1; 116 117 } 118 } 119 } 120 } 121 } 122 123 char quer[2000]; 124 125 int main() 126 { 127 memset(temp,0,sizeof(temp)); 128 temp['`']='~'; 129 temp['1']='!'; 130 temp['2']='@'; 131 temp['3']='#'; 132 temp['4']='$'; 133 temp['5']='%'; 134 temp['6']='^'; 135 temp['7']='&'; 136 temp['8']='*'; 137 temp['9']='('; 138 temp['0']=')'; 139 temp['-']='_'; 140 temp['=']='+'; 141 temp['[']='{'; 142 temp[']']='}'; 143 temp['\\']='|'; 144 temp[';']=':'; 145 temp['\'']='"'; 146 temp[',']='<'; 147 temp['.']='>'; 148 temp['/']='?'; 149 getStepArr(); 150 while(gets(quer)) 151 { 152 int len = strlen(quer); 153 int i,ans=0; 154 for(i=0;i<len;i++) quer[i]=toupper(quer[i]); 155 for(i=0;i<len-1;i++) 156 { 157 ans+=step[quer[i]][quer[i+1]]; 158 } 159 printf("%d\n",ans); 160 } 161 return 0; 162 }