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
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.

题目大意是有一个人忘记了他的手机锁屏密码,只记得他解锁的手势的先后顺序,问这个解锁顺序是不是唯一的,是唯一的输出YES,反之输出NO。

思路:题目是较为简单的思维题目,判断是否有多个相同的手势只需要考虑这个手势上下左右平移的情况即可,没有必要考虑斜向的手势。

把手机键盘模拟一下,从四个方向判断一下就可以了,如果四个方向都越界了就是表示手势是唯一的。

#include <algorithm>
#include <iostream>
#include <numeric>
#include <cstring>
#include <iomanip>
#include <string>
#include <vector>
#include <cstdio>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
#define LL long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const LL Max = 1005;
const double esp = 1e-6;
//const double PI = acos(-1);
const int INF = 0x3f3f3f3f;
using namespace std;

int arr[6][6];
void init(){
    memset(arr,-1,sizeof(arr));
    arr[1][1] = 1;arr[1][2] = 2;arr[1][3] = 3;
    arr[2][1] = 4;arr[2][2] = 5;arr[2][3] = 6;
    arr[3][1] = 7;arr[3][2] = 8;arr[3][3] = 9;
    arr[4][2] = 0;
}
int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};
char str[15];
bool finds(int n,int d){
    for(int i=0;i<6;i++){
        for(int j=0;j<6;j++){
            if(arr[i][j] == n){
                if(arr[i+dx[d]][j+dy[d]] >= 0)
                    return true;
                else
                    return false;
            }
        }
    }
}
int main(){
    int len,f;
    init();
    while(~scanf("%d",&len)){
        f = true;
        scanf("%s",str);
        for(int k=0;k<4;k++){
            int num = 0;
            for(int i=0;str[i]!='\0';i++){
                if(finds(str[i] - '0',k))
                    num += 1;
                else
                    break;
            }
            if(num == len){
                f = false;
                break;
            }
        }
        if(f)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值