【打CF,学算法——二星级】CodeForces 689A Mike and Cellphone (模拟)
2016-07-08 10:18
519 查看
【CF简介】
提交链接:CF 689A
题面:
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 considerfinger 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
Output
Input
Output
Input
Output
Input
Output
Note
You can find the picture clarifying the first sample case in the statement above.
题意:
给定一个0-9数字键盘,随后输入一个操作序列,问该操作序列在键盘上形成的手势是否是唯一的,是YES,否NO。
解题:
可以先记录初始点的位置,随后序列的位置都与该点作差,形成一个位置偏差序列。然后,暴力以0-9的位置都作为起始点开始,按照移动序列行走,若能全部走完,则计数值+1。最后看计数值是否唯一,唯一则为YES,不唯一为NO。
代码:
提交链接:CF 689A
题面:
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 considerfinger 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。
解题:
可以先记录初始点的位置,随后序列的位置都与该点作差,形成一个位置偏差序列。然后,暴力以0-9的位置都作为起始点开始,按照移动序列行走,若能全部走完,则计数值+1。最后看计数值是否唯一,唯一则为YES,不唯一为NO。
代码:
#include <iostream> #include <vector> #include <cstdio> #include <cstring> using namespace std; int x[10]={3,0,0,0,1,1,1,2,2,2},y[10]={1,0,1,2,0,1,2,0,1,2}; bool vis[4][3]; int main() { vector <int> vx,vy; int n,px,py,cnt=0; bool flag; string s; cin>>n>>s; memset(vis,-1,sizeof(vis)); vis[3][0]=0; vis[3][2]=0; px=x[s[0]-'0']; py=y[s[0]-'0']; vx.push_back(0); vy.push_back(0); for(int i=1;i<s.length();i++) { vx.push_back(x[s[i]-'0']-px); vy.push_back(y[s[i]-'0']-py); } for(int i=0;i<10;i++) { flag=1; for(int j=0;j<n;j++) { px=x[i]+vx[j]; py=y[i]+vy[j]; if(px>=0&&px<=3&&py>=0&&py<=2&&vis[px][py]) continue; else { flag=0; break; } } if(flag) cnt++; } if(cnt>1) printf("NO\n"); else printf("YES\n"); return 0; }
相关文章推荐
- PostgreSQL新手入门教程
- C#模拟http 发送post或get请求的简单实例
- Transformation 能将 Windows XP/Server 2003 操作系统,完美地模拟成 Windows Vista 的软件
- 用javascript和css模拟select的脚本
- Ajax PHP简单入门教程代码
- PHP模拟asp.net的StringBuilder类实现方法
- C#使用ImitateLogin模拟登录百度
- MySQL入门教程(七)之视图
- javascript用层模拟可移动的小窗口
- Nodejs学习笔记之入门篇
- javascript每日必学之基础入门
- React.js入门学习第一篇
- Bootstrap入门书籍之(五)导航条、分页导航
- JS模拟简易滚动条效果代码(附demo源码)
- Bootstrap入门书籍之(三)栅格系统
- Bootstrap入门书籍之(一)排版
- Javascript 基础---Ajax入门必看
- jQuery入门 构造函数
- jQuery EasyUI 入门必看
- jQuery入门介绍之基础知识