Problem description

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.

解题思路:题目的意思就是凭借手指记忆在老式键盘上按密码,如果该记忆手势产生的密码唯一,则为"YES",否则为"NO"。做法:将该记忆路径向四个方向(上下左右)各移动一格,如果都超出老式键盘的范围,说明记忆手势产生的密码唯一,则输出"YES",否则输出"NO",具体注解看代码,一遍简单过。

AC代码:

 #include<bits/stdc++.h>
using namespace std;
/*tmp[6][5]:
0 1 2 3 4
0 -1 -1 -1 -1 -1
1 -1 1 2 3 -1
2 -1 4 5 6 -1
3 -1 7 8 9 -1
4 -1 -1 -1 -1
5 -1 -1 -1 -1 -1
*/
int main(){
int n,tmp[][],cnt=,num=,dir[][]={{-,},{,},{,},{,-}};//方向数组:上右下左
char s[];bool flag;
memset(tmp,-,sizeof(tmp));tmp[][]=;
map<char,pair<int,int> > mp;//键值对,表示键盘中数字对应的坐标(first,second)
mp[''].first=,mp[''].second=;
mp[''].first=,mp[''].second=;
mp[''].first=,mp[''].second=;
mp[''].first=,mp[''].second=;
mp[''].first=,mp[''].second=;
mp[''].first=,mp[''].second=;
mp[''].first=,mp[''].second=;
mp[''].first=,mp[''].second=;
mp[''].first=,mp[''].second=;
mp[''].first=,mp[''].second=;
for(int i=;i<;++i)//tmp数组初始化
for(int j=;j<;++j)
tmp[i][j]=cnt++;
cin>>n;getchar();//吃掉回车符对字符串的影响
cin>>s;
for(int x=;x<;++x){//枚举四个方向
flag=false;
for(int j=;j<n;++j)
if(tmp[mp[s[j]].first+dir[x][]][mp[s[j]].second+dir[x][]]<){flag=true;break;}
if(flag){num++;}//只要小于0,即超出老式键盘的范围,计数器就加1
}
if(num==)cout<<"YES"<<endl;//只要向4个方向移动一格后都超出老式键盘的范围,说明记忆手势产生唯一的按键密码,则该密码正确
else cout<<"NO"<<endl;//否则还有其他不确定的密码,则为NO
return ;
}

B - Mike and Cellphone(map)的更多相关文章

  1. GO语言总结(4)——映射(Map)

    上一篇博客介绍了Go语言的数组和切片——GO语言总结(3)——数组和切片,本篇博客介绍Go语言的映射(Map) 映射是一种内置的数据结构,用来保存键值对的无序集合. (1)映射的创建 make ( m ...

  2. Java-集合=第五题 (Map)设计Account 对象如下: private long id; private double balance; private String password; 要求完善设计,使得该Account 对象能够自动分配id。 给定一个List 如下: List list = new ArrayList(); list.add(new A

    第五题 (Map)设计Account 对象如下: private long id; private double balance; private String password; 要求完善设计,使得 ...

  3. Java-map-第一题 (Map)利用Map,完成下面的功能: 从命令行读入一个字符串,表示一个年份,输出该年的世界杯冠军是哪支球队。如果该 年没有举办世界杯,则输出:没有举办世界杯。 附:世界杯冠军以及对应的夺冠年份,请参考本章附录。 附录

    第一题 (Map)利用Map,完成下面的功能: 从命令行读入一个字符串,表示一个年份,输出该年的世界杯冠军是哪支球队.如果该 年没有举办世界杯,则输出:没有举办世界杯. 附:世界杯冠军以及对应的夺冠年 ...

  4. 第一题 (Map)利用Map,完成下面的功能:

    从命令行读入一个字符串,表示一个年份,输出该年的世界杯冠军是哪支球队.如果该 年没有举办世界杯,则输出:没有举办世界杯.  附:世界杯冠军以及对应的夺冠年份,请参考本章附录. 附录  1.历届世界杯冠 ...

  5. 【机器学习基本理论】详解最大似然估计(MLE)、最大后验概率估计(MAP),以及贝叶斯公式的理解

    [机器学习基本理论]详解最大似然估计(MLE).最大后验概率估计(MAP),以及贝叶斯公式的理解 https://mp.csdn.net/postedit/81664644 最大似然估计(Maximu ...

  6. 【机器学习基本理论】详解最大后验概率估计(MAP)的理解

    [机器学习基本理论]详解最大后验概率估计(MAP)的理解 https://blog.csdn.net/weixin_42137700/article/details/81628065 最大似然估计(M ...

  7. GoLang基础数据类型--->字典(map)详解

    GoLang基础数据类型--->字典(map)详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.   可能大家刚刚接触Golang的小伙伴都会跟我一样,这个map是干嘛的,是 ...

  8. 列表生成式+过滤器(filter)+映射(map)+lambda总结

    这些都是python的特色,不仅强大,而且好用,配合起来使用更是无敌. 零.lambda lambda用于产生一个匿名表达式,组成部分为:lambda + ‘函数表达式’ ‘函数表达式’由一个冒号加上 ...

  9. 最大似然估计(MLE)与最大后验概率(MAP)

    何为:最大似然估计(MLE): 最大似然估计提供了一种给定观察数据来评估模型参数的方法,即:“模型已定,参数未知”.可以通过采样,获取部分数据,然后通过最大似然估计来获取已知模型的参数. 最大似然估计 ...

随机推荐

  1. 浅谈Java三大框架与应用

    前言:对于一个程序员来说,尤其是在java web端开发的程序员,三大框架:Struts+Hibernate+Spring是必须要掌握熟透的,因此,下面谈谈java三大框架的基本概念和原理. JAVA ...

  2. jQuery顺序加载图片(终版)

    这一篇是对上一篇(jQuery顺序加载图片(初版)--http://www.cnblogs.com/newbie-cc/p/3707504.html)的改进. function loadImage(i ...

  3. Higher-Order Functions and Lambdas

    https://kotlinlang.org/docs/reference/lambdas.html

  4. Overview of Polymorphism -多态的分类

    多态有类型系统衍生. 有限类型.无限类型.确定类型. Classifications Christopher Strachey (1967) introduced the concept of pol ...

  5. mac 上执行 rm -rf /

    # 很可怕的指令,清空磁盘所有资料,千万不要用 sudo 尝试,吓的小心肝差掉跳出来 rm -rf / 无聊,想执行rm -rf /会怎样,想起没加sudo时对~/download执行提示权限不足,被 ...

  6. DOCKER - J2EE中容器:WEB容器、EJB容器

    转自:http://www.voidcn.com/article/p-yizkqdxp-zg.html

  7. 单点登录之 CAS SSO 从入门到精通(第一天)

    转自:http://blog.csdn.net/lifetragedy/article/details/43817903 啊......it's quite a long time. 好久没更新博客了 ...

  8. 59.bouncing results

        一.bouncing results成因及解决方案 bouncing results问题,两个document排序,field值相同:不同的shard上,可能排序不同:每次请求轮询路由到不同的 ...

  9. 【codeforces 793A】Oleg and shares

    [题目链接]:http://codeforces.com/contest/793/problem/A [题意] 每次你可以对1..n中的任意一个数字进行减少k操作; 问你最后可不可能所有的数字都变成一 ...

  10. 第三篇:SpringBoot - 数据库结构版本管理与迁移

    SpringBoot支持了两种数据库结构版本管理与迁移,一个是flyway,一个是liquibase.其本身也支持sql script,在初始化数据源之后执行指定的脚本,本章是基于 Liquibase ...