A. Mike and Cellphone(Round 361 Div.2)
写一半去开程序的时候不小心关了网页,LOFTER我都不奢望加入代码高亮,最起码我关闭的时候弹个对话框,再不济也给我定时保存一下草稿吧。
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.
·
题意: 对于给定手机键盘上这n个数,手势是否唯一确定这一个数?是的话输出YES,否则输出NO。
这道题一个星期前看到的,当时觉得好难,想了好几个方法都觉得是自己在乱搞,没有什么完整性。
于是上网看了题解后自己实现。
题解参考资料:
http://www.cnblogs.com/q-c-y/p/5660107.html
http://www.voidcn.com/blog/David_Jett/article/p-6089310.html
——————
方法1:
初始化一个数组用来存放每个数和起点的差值,
然后枚举0 - 9 每个数按差值走是否合法,如果有多个合法则为NO,否则为YES。
代码如下:
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std ;
struct node {
int x,y;
};
node a[] = {3,1,0,0,0,1,0,2,1,0,1,1,1,2,2,0,2,1,2,2};
// 以键盘1为坐标(0,0) 建立坐标系,每个数字坐标
int main()
{
string s ;
int n ;
cin >> n >> s ;
node b[10] ; // 用来存储每个数字与起点直接的差值
node origin ; // 起点
int cnt = 0 ; // 可行数;
int vis[4][3] ;
memset( vis , -1 , sizeof(vis) ) ;
vis[3][0] = 0 ;
vis[3][2] = 0 ;
origin.x = a[ s[0] - '0' ] .x ;
origin.y = a[ s[0] - '0' ] .y ;
for( int i = 0 ; i < n ; i++ ) {
b[i].x = a[ s[i] - '0' ].x - origin.x ;
b[i].y = a[ s[i] - '0' ].y - origin.y ;
}
//px , py 为枚举时当前坐标位置
int px , py ;
for( int i = 0 ; i <= 9 ; i++ ) {
int flag = 1 ;
for( int j = 0 ; j< n ; j++ ) {
px = a[i].x + b[j].x ;
py = a[i].y + b[j].y ;
if( px >= 0 && px <= 3 && py >=0 && py <= 2
&& vis[px][py] )
continue ;
else {
flag = 0 ;
cout << px << ' ' << py << endl ;
break;
}
}
if( flag == 1)
cnt++;
}
if( cnt == 1) cout << "YES" << endl;
else cout << "NO" << endl ;
return 0 ;
}
(1)
学到了通过memset( vis , -1 , sizeof( vis) ) 的方法,以前的我可能会用两个循环赋1,学到了-1一样可以这样的效果(memset只能初始化数组为-1 或者0 )
学到了sizeof(vis) 的表示方法,如果是我以前可能会用sizeof( int ) * 3 * 4
(2)
对于布尔代数一直小瞧了,一开始写过一个版本的:
if( px >= 0 && px <= 3 && py >=0 && py <= 2
&& ( px != 3 && py != 0 ) && ( px != 3 && py != 2 )
结果样例都过不了,后来改成:
if( px >= 0 && px <= 3 && py >=0 && py <= 2
&& ( px != 3 && (py != 0 || py != 2 ) )
WA在test10
数据:
Input
2 10
Output
NO
Answer
YES
看起来是0有关的问题。
明明读起来自己很顺口的 , “px不等于3并且py不等于2” 这样的。还是有点小瞧逻辑学了,以后注意这方面。
方法2:
如果有多解,那么一定可以通过对其中一个解进行上下左右平移等到其他解,所以本题思路是对边界判定,如果数列中出现了边界数字,则显然不能向某个方向移动。
代码如下:
#include<iostream>
#include<cstring>
using namespace std ;
int main()
{
int n ;
string s ;
cin >> n >> s ;
int L=1,R=1,U=1,D=1;
for( int i = 0 ; i < n ; i++ ) {
if( s[i] - '0' == 1 || s[i] - '0' == 2 || s[i] - '0' == 3 )
U = 0 ;
if(s[i] - '0' == 1 || s[i] - '0' == 4 || s[i] - '0' == 7 )
L = 0 ;
if(s[i] - '0' == 3 || s[i] - '0' == 6 || s[i] - '0' == 9 )
R = 0;
if(s[i] - '0' == 0)
L=0,R=0,D=0;
if(s[i] - '0' == 7 || s[i] - '0' == 9 )
D = 0 ;
}
if( U == 0 && L == 0 && R == 0 && D == 0 )
cout <<"YES"<< endl ;
else cout << "NO" << endl ;
}
最后:
![]()
A. Mike and Cellphone(Round 361 Div.2)的更多相关文章
- Codeforces Round #361 (Div. 2) A. Mike and Cellphone 水题
A. Mike and Cellphone 题目连接: http://www.codeforces.com/contest/689/problem/A Description While swimmi ...
- Codeforces Round #361 (Div. 2)A. Mike and Cellphone
A. Mike and Cellphone time limit per test 1 second memory limit per test 256 megabytes input standar ...
- Codeforces Round #361 (Div. 2) A
A - Mike and Cellphone Description While swimming at the beach, Mike has accidentally dropped his ce ...
- Codeforces Round #361 (Div. 2) 套题
A - Mike and Cellphone 问有没有多解,每个点按照给出的序列用向量法跑一遍 #include<cstdio> #include<cstring> #incl ...
- CodeForces 689A Mike and Cellphone (模拟+水题)
Mike and Cellphone 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/E Description While sw ...
- Codeforces Round #361 (Div. 2)——B. Mike and Shortcuts(BFS+小坑)
B. Mike and Shortcuts time limit per test 3 seconds memory limit per test 256 megabytes input standa ...
- Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 离散化 排列组合
E. Mike and Geometry Problem 题目连接: http://www.codeforces.com/contest/689/problem/E Description Mike ...
- Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 【逆元求组合数 && 离散化】
任意门:http://codeforces.com/contest/689/problem/E E. Mike and Geometry Problem time limit per test 3 s ...
- Codeforces Round #361 (Div. 2) C. Mike and Chocolate Thieves 二分
C. Mike and Chocolate Thieves 题目连接: http://www.codeforces.com/contest/689/problem/C Description Bad ...
随机推荐
- Buffett saying
1. 人生财富就像滚雪球,最重要的是发现很湿的雪和很长的坡. 2. 雪球想滚大,必须要有最坚实的核心:一生坚持的价值投资理念 价值投资一直是巴菲特投资理念的核心,他始终认为投资企业最重要的是要看准企业 ...
- PQ分区魔术师v9.0 中文版
软件名称: pqmagic 硬盘分区大师9.0中文绿色版 软件大小:5.80MB 软件语言:简体中文 软件类别:磁盘工具 软件授权:免费软件 更新时间:2013-10-082013-10-08 09: ...
- echarts样式修改
本人是查看如下链接: http://down.admin5.com/demo/code_pop/cs/dsj/doc/example/themeDesigner.html# 图示很简洁明了.
- HDU 1517 A Multiplication Game 博弈
题目大意:从1开始Stan与Ollie经行博弈,stan先手,每次将当前数乘上(2~9)间的任意数,最后一次操作后大于等于n的人获胜. 题目思路: 1-9 stan 胜 10-18 ollie胜 19 ...
- Android:Asmack能登录但是获取不到联系人的问题
先说一下:换了N个jar包,换了N个OpenFire,就是获取不到联系人.但是能登录.特别纳闷,百度不到,google一下,有人隐约说了下权限的问题. 终于搞出来了,总结一下,浪费了一整天的时间.(码 ...
- 把Excel工作簿的每个工作表提取出来保存为新工作簿
平台:MS office 2010 任务:有个excel工作簿,其中有上百个工作表,要求把每一个工作表全部保存为新工作簿,如果一个一个复制出来太傻了,可以用excel自带的VB解决. 方法:打开工作簿 ...
- fpSpread 设置Border 样式
// Create a new bevel border. //FarPoint.Win.BevelBorder bevelbrdr = new FarPoint.Win.BevelBorder(Fa ...
- Strusts2--课程笔记7
国际化: 国际化是指,使程序在不做任何修改的情况下,就可以使用在不同的语言环境中.国际化在一般性项目中是不常用的.在编程中简称 i18n. 国际化是通过读取资源文件的形式实现的.资源文件的定义与注册, ...
- 分布式版本控制系统Git-----3.图形化Tortoisegit创建本地库并且提交到远程服务器上
[前提你已经有了自己的远程仓库帐号密码和HTTP地址] PS:图形化会方便,但是个人认为还是敲命令比较好,会锻炼人的思维和逻辑. 1.首先在任意一个地方创建test目录.若要把test目录放在Git的 ...
- 实验--DHCP服务器搭建
系统环境:CentOS PC1: 客户端1(克隆CentOS) PC2: 客户端2(克隆CentOS) Router: 模拟路由器(克隆CentOS)