codeforces 361 A

原题:

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.

Sample Input
Input
3
586
Output
NO
Input
2
09
Output
NO
Input
9
123456789
Output
YES
Input
3
911
Output
YES
原题

 

 

提示:当当前手势可以左移一个或者右、上、下 ,即代表不唯一,输出NO。暴力判断就好,比如包含1,4,7,0 之一的number就是不可以左移的。如果四个方向都不可移动就是YES。

 

代码:

 

#include<cstdio>
#include<cstring>
#include<iostream>

using namespace std;

#define  MAX(x,y) (((x)>(y)) ? (x) : (y))
#define  MIN(x,y) (((x) < (y)) ? (x) : (y))
#define ABS(x) ((x)>0?(x):-(x))

const int inf = 0x7fffffff;
const int N=10;
char a[N];
int b[N];

int n;

bool can_move_left()
{
    for(int i=0;i<n; i++){
        if(b[i]==1 ||b[i]==4 ||b[i]==7 ||b[i]==0)
            return false;
    }
    return true;
}

bool can_move_right()
{
    for(int i=0;i<n; i++){
        if(b[i]==3 ||b[i]==6 ||b[i]==9 ||b[i]==0)
            return false;
    }
    return true;
}

bool can_move_up()
{
    for(int i=0;i<n; i++){
        if(b[i]==1 ||b[i]==2 ||b[i]==3)
            return false;
    }
    return true;
}

bool can_move_down()
{
    for(int i=0;i<n; i++){
        if(b[i]==7 ||b[i]==0 ||b[i]==9)
            return false;
    }
    return true;
}

int main()
{

    cin>>n;
    scanf("%s",a);
    for(int i=0; i<n; i++){
        b[i]=a[i]-'1'+1;
    }

    int ok=0;
    if(ok == 0){
        if(can_move_left())       ok=1;
    }
    if(ok == 0){
        if(can_move_right())       ok=1;
    }
    if(ok == 0){
        if(can_move_up())       ok=1;
    }
    if(ok == 0){
        if(can_move_down())       ok=1;
    }


    printf("%s
",ok? "NO":"YES");





    return 0;
}

 

原文地址:https://www.cnblogs.com/shawn-ji/p/5661625.html