链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=121473#problem/A
While swimming at the beach, Mike has accidentally dropped his cellphone into the water. There was no worry as he bought a cheap replacement phone with an old-fashioned keyboard. The keyboard has only ten digital equal-sized keys, located in the following way:
Together with his old phone, he lost all his contacts and now he can only remember the way his fingers moved when he put some number in. One can formally consider finger movements as a sequence of vectors connecting centers of keys pressed consecutively to put in a number. For example, the finger movements for number "586" are the same as finger movements for number "253":
Mike has already put in a number by his "finger memory" and started calling it, so he is now worrying, can he be sure that he is calling the correct number? In other words, is there any other number, that has the same finger movements?
Input
The first line of the input contains the only integer n (1 ≤ n ≤ 9) — the number of digits in the phone number that Mike put in.
The second line contains the string consisting of n digits (characters from '0' to '9') representing the number that Mike put in.
Output
If there is no other phone number with the same finger movements and Mike can be sure he is calling the correct number, print "YES" (without quotes) in the only line.
Otherwise print "NO" (without quotes) in the first line.
Sample Input
3
586
NO
2
09
NO
9
123456789
YES
3
911
YES
题意:如果手指行走路线只有一条,就可以确定数值,输出YES,否者输出NO。
解析:有两种方法
1、计算两点之间的差额,从头开始,直接寻找。
源代码:
1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 struct node{ 5 int x,y; 6 }a[9]; 7 int main() 8 { 9 node a[]={3,1,0,0,0,1,0,2,1,0,1,1,1,2,2,0,2,1,2,2};//每个数所在的位置 10 int d[4][3]={1,2,3,4,5,6,7,8,9,-1,0,-1};//每个位置上的数 11 int n; 12 int b[10][2]; 13 char c[10]; 14 scanf("%d",&n); 15 int k=0; 16 scanf("%s",c); 17 for(int i=1;i<n;i++) 18 { 19 b[k][0]=a[c[i]-'0'].x-a[c[i-1]-'0'].x; //更改的x值 20 b[k][1]=a[c[i]-'0'].y-a[c[i-1]-'0'].y; //更改的y值 21 k++; 22 } 23 int i,j,t=0; 24 for(i=0;i<=9;i++) 25 { 26 int s=i; 27 for(j=0;j<k;j++) 28 { 29 int xx=a[s].x+b[j][0]; 30 int yy=a[s].y+b[j][1]; 31 if(xx>=0&&xx<4&&yy>=0&&yy<3&&d[xx][yy]>=0) //确定是否可以移动 32 s=d[xx][yy]; //更改第一个点 33 else 34 break; 35 } 36 if(j>=k) //有相同路径 37 t++; 38 } 39 if(t>1) 40 printf("NO "); 41 else 42 printf("YES "); 43 return 0; 44 }
2、设上下左右方向的向量分别为U、D、L、R,当不可向该方向移动时对应的字母值为1。例如当操作序列含0时,左右下都不可移动,则L=R=D=0;
当所有方向都不可走时,即手势唯一
源代码:
1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 int main(){ 5 int n; 6 char s[10000]; 7 while(cin>>n>>s) 8 { 9 int U=0,D=0,L=0,R=0; 10 for(int i=0;i<n;i++) 11 { 12 if(s[i]=='0') 13 D=R=L=1; 14 if(s[i]=='1'||s[i]=='2'||s[i]=='3') 15 U=1;//上 16 if(s[i]=='1'||s[i]=='4'||s[i]=='7') 17 L=1;//左 18 if(s[i]=='3'||s[i]=='6'||s[i]=='9') 19 R=1;//右 20 if(s[i]=='7'||s[i]=='9') 21 D=1;//下 22 } 23 if(U==1&&D==1&&R==1&&L==1) 24 cout<<"YES"<<endl; 25 else 26 cout<<"NO"<<endl; 27 } 28 return 0; 29 }