B - Mike and Cellphone(map)
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)的更多相关文章
- GO语言总结(4)——映射(Map)
上一篇博客介绍了Go语言的数组和切片——GO语言总结(3)——数组和切片,本篇博客介绍Go语言的映射(Map) 映射是一种内置的数据结构,用来保存键值对的无序集合. (1)映射的创建 make ( m ...
- 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; 要求完善设计,使得 ...
- Java-map-第一题 (Map)利用Map,完成下面的功能: 从命令行读入一个字符串,表示一个年份,输出该年的世界杯冠军是哪支球队。如果该 年没有举办世界杯,则输出:没有举办世界杯。 附:世界杯冠军以及对应的夺冠年份,请参考本章附录。 附录
第一题 (Map)利用Map,完成下面的功能: 从命令行读入一个字符串,表示一个年份,输出该年的世界杯冠军是哪支球队.如果该 年没有举办世界杯,则输出:没有举办世界杯. 附:世界杯冠军以及对应的夺冠年 ...
- 第一题 (Map)利用Map,完成下面的功能:
从命令行读入一个字符串,表示一个年份,输出该年的世界杯冠军是哪支球队.如果该 年没有举办世界杯,则输出:没有举办世界杯. 附:世界杯冠军以及对应的夺冠年份,请参考本章附录. 附录 1.历届世界杯冠 ...
- 【机器学习基本理论】详解最大似然估计(MLE)、最大后验概率估计(MAP),以及贝叶斯公式的理解
[机器学习基本理论]详解最大似然估计(MLE).最大后验概率估计(MAP),以及贝叶斯公式的理解 https://mp.csdn.net/postedit/81664644 最大似然估计(Maximu ...
- 【机器学习基本理论】详解最大后验概率估计(MAP)的理解
[机器学习基本理论]详解最大后验概率估计(MAP)的理解 https://blog.csdn.net/weixin_42137700/article/details/81628065 最大似然估计(M ...
- GoLang基础数据类型--->字典(map)详解
GoLang基础数据类型--->字典(map)详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 可能大家刚刚接触Golang的小伙伴都会跟我一样,这个map是干嘛的,是 ...
- 列表生成式+过滤器(filter)+映射(map)+lambda总结
这些都是python的特色,不仅强大,而且好用,配合起来使用更是无敌. 零.lambda lambda用于产生一个匿名表达式,组成部分为:lambda + ‘函数表达式’ ‘函数表达式’由一个冒号加上 ...
- 最大似然估计(MLE)与最大后验概率(MAP)
何为:最大似然估计(MLE): 最大似然估计提供了一种给定观察数据来评估模型参数的方法,即:“模型已定,参数未知”.可以通过采样,获取部分数据,然后通过最大似然估计来获取已知模型的参数. 最大似然估计 ...
随机推荐
- ANN:DNN结构演进History—LSTM网络
为了保持文章系列的连贯性,参考这个文章: DNN结构演进History-LSTM_NN 对于LSTM的使用:谷歌语音转录背后的神经网络 摘要: LSTM使用一个控制门控制参数是否进行梯度计算,以此避免 ...
- C++程序设计实验安排
2016-2017第二学期C++程序设计的实验时间与地点安排如下表,请大家根据时间按时来上机实验.另外,因为原来安排在4.1的实验因为调休补周一的课,因此挪至周五.另外第4次周六的课,考虑有一些同学有 ...
- ABP(http://www.aspnetboilerplate.com/)下载初始化
官网:http://www.aspnetboilerplate.com/ 下载 下载完成后用vs2015打开,是2015,低版本打开可能会出现一些问题 生成项目,Nuget会自动下载需要的类库 ABP ...
- C#—接口和抽象类的区别?
一.接口 接口是指对协定进行定义的引用类型,其他类型实现接口,以保证它们支持某些操作.接口指定必须由类提供的成员或实现它的其他接口.与类相似,接口可以包含方法.属性.索引器和事件作为成员. 1.接口存 ...
- 多叉树结构的数据,parent表示法转成children表示法
最近碰到的问题,有个数组,数组元素是对象,该对象的结构就如树的parent表示法的节点一样.形象点讲就是该数组存放了树的所有“叶子节点”,并且叶子节点内存有父节点,一直到根节点为止,就如存了一条从叶子 ...
- elasticsearch 权威指南排序阅读笔记(六)
默认排序 默认查询是通过_source 准确性权重来排序 字段排序 { "query":{ "match":{ "productName": ...
- POJ 2029
二维树状数组可解此题 #include <iostream> #include <cstdio> #include <cstring> #include <a ...
- MySQL经常使用命令--show命令使用
log into the mysql for localhost mysql -u username -ppasswd(there is no space) for ip mysql -h ip -P ...
- Swift中文教程(二)基本运算符
1.基本运算符 运算符是一种特定的符号或表达式,用来检验.改动或合并变量.比如,用求和运算符+能够对两个数字进行求和(如let i = 1 + 2):略微复杂一点的样例有逻辑与操作符&& ...
- Openstack针对nova,cinder,glance使用ceph的虚拟机创建机制优化
今天在开源中国社区看到有例如以下一个问题: 已经成功把ceph作为cinder和 glance的后端,可是假设作为nova的后端,虚拟机启动速度非常慢,网上查了一下是由于openstack创建虚 ...