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程序员级别划分

    IT路虽好,却难走.1级   为会基本语法 大学里的JAVA教程 能及格 2级   自己可以写个 俄罗斯方块,扫雷,贪吃蛇, 拼图之类的小游戏 3级   能够进手机游戏CP,SP公司,做手机游戏 或者 ...

  2. C#的WebBrowser操作frame

    刚学c#不久,也不太懂什么IHTMLDocument.IHTMLDocument2.IWebBrowser2等等.自己琢磨了好久,终于知道了怎么用WebBrowser操作frame和iframe. 1 ...

  3. vue中用v-for循环出出来的div下面的span不给宽度也能相对于div居中

    效果图 1.html <div> <div v-on:mousemove="dataDetails($event, item)" v-on:mouseleave= ...

  4. PAT_A1126#Eulerian Path

    Source: PAT A1126 Eulerian Path (25 分) Description: In graph theory, an Eulerian path is a path in a ...

  5. TensorFlow的序列模型代码解释(RNN、LSTM)---笔记(16)

    1.学习单步的RNN:RNNCell.BasicRNNCell.BasicLSTMCell.LSTMCell.GRUCell (1)RNNCell 如果要学习TensorFlow中的RNN,第一站应该 ...

  6. S-HR薪酬项目与核算表的关系

  7. Cocos2d+C++运行出现中断的解决方法

    原因是引入外部的文件不存在问题,一般是路径问题,例如引入的图片文件路径不存在.

  8. git常见问题总结

    1.每次上传文件的时候,有很多iml文件容易不小心上传上去,然后报错,所以可以把这些文件取消上传 如图所示,每次提交时,都不会显示标红文件 具体操作步骤如下:

  9. QT创建快捷方式

    1.当我第一次安装完QT后发现它并没有在桌面上给我创建快捷方式,使用起来很难受啊,于是寻找办法,终于找到一个懒办法: 首先进入到上图的BIN目录下找到qtcreator文件,双击运行,然后系统就会在左 ...

  10. (33)Spring Boot 监控和管理生产环境【从零开始学Spring Boot】

    [本文章是否对你有用以及是否有好的建议,请留言] spring-boot-actuator模块提供了一个监控和管理生产环境的模块,可以使用http.jmx.ssh.telnet等拉管理和监控应用.审计 ...