【打CF,学算法——二星级】CodeForces 689A Mike and Cellphone (模拟)

来源:互联网 发布:出版行业 知乎 编辑:程序博客网 时间:2024/05/17 13:43

【CF简介】

提交链接:CF 689A


题面:

A. Mike and Cellphone
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 considerfinger 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.

Examples
Input
3586
Output
NO
Input
209
Output
NO
Input
9123456789
Output
YES
Input
3911
Output
YES
Note

You can find the picture clarifying the first sample case in the statement above.

题意:

     给定一个0-9数字键盘,随后输入一个操作序列,问该操作序列在键盘上形成的手势是否是唯一的,是YES,否NO。


解题:

     可以先记录初始点的位置,随后序列的位置都与该点作差,形成一个位置偏差序列。然后,暴力以0-9的位置都作为起始点开始,按照移动序列行走,若能全部走完,则计数值+1。最后看计数值是否唯一,唯一则为YES,不唯一为NO。


代码:

#include <iostream>#include <vector>#include <cstdio>#include <cstring>using namespace std;int x[10]={3,0,0,0,1,1,1,2,2,2},y[10]={1,0,1,2,0,1,2,0,1,2};bool vis[4][3];int main(){vector <int> vx,vy;    int n,px,py,cnt=0;bool flag;string s;cin>>n>>s;memset(vis,-1,sizeof(vis));vis[3][0]=0;vis[3][2]=0;px=x[s[0]-'0'];py=y[s[0]-'0'];vx.push_back(0);vy.push_back(0);for(int i=1;i<s.length();i++){    vx.push_back(x[s[i]-'0']-px);vy.push_back(y[s[i]-'0']-py);}for(int i=0;i<10;i++){flag=1;for(int j=0;j<n;j++){px=x[i]+vx[j];py=y[i]+vy[j];if(px>=0&&px<=3&&py>=0&&py<=2&&vis[px][py])  continue;else{flag=0;break;}}if(flag)cnt++;}if(cnt>1)printf("NO\n");elseprintf("YES\n");return 0;}


0 0
原创粉丝点击