您的位置:首页 > 其它

Mike and Cellphone

2016-07-11 14:21 274 查看
链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=121473#problem/AMike 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?InputThe 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.OutputIf 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 InputInput
3
586
Output
NO
Input
2
09
Output
NO
Input
9
123456789
Output
YES
Input
3
911
Output
YES

题意:如果手指行走路线只有一条,就可以确定数值,输出YES,否者输出NO。
解析:有两种方法
1、计算两点之间的差额,从头开始,直接寻找。
源代码:
#include<iostream>
#include<cstdio>
using namespace std;
struct node{
int x,y;
}a[9];
int main()
{
node a[]={3,1,0,0,0,1,0,2,1,0,1,1,1,2,2,0,2,1,2,2};//每个数所在的位置
int d[4][3]={1,2,3,4,5,6,7,8,9,-1,0,-1};//每个位置上的数
int n;
int b[10][2];
char c[10];
scanf("%d",&n);
int k=0;
scanf("%s",c);
for(int i=1;i<n;i++)
{
b[k][0]=a[c[i]-'0'].x-a[c[i-1]-'0'].x;   //更改的x值
b[k][1]=a[c[i]-'0'].y-a[c[i-1]-'0'].y;    //更改的y值
k++;
}
int i,j,t=0;
for(i=0;i<=9;i++)
{
int s=i;
for(j=0;j<k;j++)
{
int xx=a[s].x+b[j][0];
int yy=a[s].y+b[j][1];
if(xx>=0&&xx<4&&yy>=0&&yy<3&&d[xx][yy]>=0)    //确定是否可以移动
s=d[xx][yy];   //更改第一个点
else
break;
}
if(j>=k)   //有相同路径
t++;
}
if(t>1)
printf("NO\n");
else
printf("YES\n");
return 0;
}
2、设上下左右方向的向量分别为U、D、L、R,当不可向该方向移动时对应的字母值为1。例如当操作序列含0时,左右下都不可移动,则L=R=D=0;当所有方向都不可走时,即手势唯一源代码:
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
int n;
char s[10000];
while(cin>>n>>s)
{
int U=0,D=0,L=0,R=0;
for(int i=0;i<n;i++)
{
if(s[i]=='0')
D=R=L=1;
if(s[i]=='1'||s[i]=='2'||s[i]=='3')
U=1;//上
if(s[i]=='1'||s[i]=='4'||s[i]=='7')
L=1;//左
if(s[i]=='3'||s[i]=='6'||s[i]=='9')
R=1;//右
if(s[i]=='7'||s[i]=='9')
D=1;//下
}
if(U==1&&D==1&&R==1&&L==1)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: