Mike and Cellphone
鏈接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=121473#problem/A
Mike and Cellphone ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Time Limit:1000MS?????Memory Limit:262144KB?????64bit IO Format:%I64d & %I64uWhile 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
Input 3586 Output NO Input 2
09 Output NO Input 9
123456789 Output YES Input 3
911 Output 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\n"); 41 else 42 printf("YES\n"); 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 }?
轉載于:https://www.cnblogs.com/q-c-y/p/5660107.html
總結
以上是生活随笔為你收集整理的Mike and Cellphone的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大额行号
- 下一篇: CAD坐标标注中如何标注普通坐标?