您的位置:首页 > 其它

Codeforces Round #361 (Div. 2) 689A Mike and Cellphone

2016-09-26 23:53 471 查看
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 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.

Examples

Input

3

586

Output

NO

Input

2

09

Output

NO

Input

9

123456789

Output

YES

Input

3

911

Output

YES

Note

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

题意:

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

题解:

记录初始点位置,然后记录序列每位与初始位的横坐标纵坐标差(用别的方法记,比如欧几里得距离也可以)

枚举起始点,按记录的差值走,找满足条件的输出‘NO’,假设1在(1,1)注意0这个点在(4,2)这个位置。

#include<iostream>
#include<cstring>
#include<vector>
#include<cstdio>
#include<cstdlib>
using namespace std;
vector<pair<int,int> >v;
pair<int,int>x,a,first;
int main()
{
int n,u,i,j;
char u_;
cin>>n>>u_;
u=u_-'0';
if(u==0)//初始点,特判0
{a.first=4;a.second=2;}
else
{a.first=(u-1)/3+1;a.second=u%3==0?3:u%3;}
first=a;
for(i=2;i<=n;i++)
{
cin>>u_;
u=u_-'0';
//记录差值
if(u==0){v.push_back(make_pair(4-a.first,2-a.second));continue;}
x.first=(u-1)/3+1;x.second=u%3==0?3:u%3;v.push_back(make_pair(x.first-a.first,x.second-a.second));
}
for(i=0;i<=9;i++)//枚举起始点
{
if(i==0)
{a.first=4;a.second=2;}
else
{a.first=(i-1)/3+1;a.second=i%3==0?3:i%3;}
if(a.first==first.first&&a.second==first.second)continue;
for(j=0;j<v.size();j++)//沿着差值走
{
x.first=a.first+v[j].first;x.second=a.second+v[j].second;
if((x.first<1||x.first>3||x.second<1||x.second>3)&&(!(x.first==4&&x.second==2)))break;
}
if(j==v.size())
{cout<<"NO";return 0;}
}
cout<<"YES";
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces 枚举 模拟