Codeforces Round #361 (Div. 2) A. Mike and Cellphone ( 模拟 )

来源:互联网 发布:淘宝怎样改评价 编辑:程序博客网 时间:2024/05/18 00:29
A. Mike and Cellphone
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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.

题意:

 给定一个0-9数字键盘,随后输入一个操作序列,

问该操作序列在键盘上形成的手势是否是唯一的,是YES,否NO。

计算起点与其他点位置之间的差额(方向队列记录),从头开始,直接寻找,模拟过程;

AC代码:

#include <cstdio>#include <iostream>#include<algorithm>#include<string>#include<queue>struct  node{int x;int y;};using namespace std;int ans[6][5] = {-1,-1,-1,-1,-1,-1 ,1, 2, 3,-1,-1, 4, 5, 6,-1,-1, 7, 8, 9,-1,-1,-1, 0,-1,-1,-1,-1,-1,-1,-1};int main(){int n; node q[10];q[0].x = 4; q[0].y = 2;q[1].x = 1; q[1].y = 1;q[2].x = 1; q[2].y = 2;q[3].x = 1; q[3].y = 3;q[4].x = 2; q[4].y = 1;q[5].x = 2; q[5].y = 2;q[6].x = 2; q[6].y = 3;q[7].x = 3; q[7].y = 1;q[8].x = 3; q[8].y = 2;q[9].x = 3; q[9].y = 3;while (scanf("%d",&n)!=EOF) {node b;queue<node>dir;char a[100];int k;scanf("%s", a);for (int i = 0; i < n; i++){int x = a[i] - '0';if (i == 0) { b = q[x]; k = x; }else{node task; task.x = q[x].x - b.x; task.y = q[x].y - b.y; dir.push(task);}}int count;for (int i = 0; i <= 10; i++){if (k == i) continue;queue<node>f = dir; count = 0;while (!f.empty()){node task = f.front();   f.pop();int xx = q[i].x + task.x; int yy = q[i].y + task.y;if (xx >= 1 && xx <= 4 && yy >= 1 && yy<=3 && ans[xx][yy]!=-1 ){count++;} if (count == n - 1) break;}if (count ==  n - 1) break;}if (count ==  n - 1)    printf("NO\n");else                    printf("YES\n");}return 0;} 


0 0
原创粉丝点击