解治

导航

Codeforces Round #361 Jul.6th A题 ☺Code

#官方Code#

#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
using namespace std;
int v[55][55];
string number;
int len;
pair <int,int > s[55][55];
pair <int,int > where[55];
int main()
{
    int i,j,k;
    for(i=1;i<=10;i++)
        for(j=1;j<=10;j++)
            v[i][j] = -1;
    v[1][1] = 1;
    v[1][2] = 2;
    v[1][3] = 3;
    v[2][1] = 4;
    v[2][2] = 5;
    v[2][3] = 6;
    v[3][1] = 7;
    v[3][2] = 8;
    v[3][3] = 9;
    v[4][2] = 0;
    for(k=0;k<=9;k++)
        for(i=1;i<=4;i++)
            for(j=1;j<=4;j++)
                if(v[i][j]==k)
                {                    
                    where[k].first=i;
                    where[k].second=j;
                }
    for(i=0;i<=9;i++)
        for(j=0;j<=9;j++)
        {
            s[i][j].first=where[i].first-where[j].first;
            s[i][j].second=where[i].second-where[j].second;
        }
    scanf("%d",&len);
    cin>>number;
    vector <string> ans;
    for(k=0;k<=9;k++)
        if (k!=number[0]-'0')
        {
            string count="";
            count+=k+'0';
            pair<int ,int > pos=where[k];
            for(i=0;i<len-1;i++)
            {
                pos.first+=s[number[i+1]-'0'][number[i]-'0'].first;
                pos.second+=s[number[i+1]-'0'][number[i]-'0'].second;
                if ((1<=pos.first)&&(pos.first<=4)&&(1<=pos.second)&&(pos.second<=3)&&(v[pos.first][pos.second]!=-1))
                    count+=v[pos.first][pos.second]+'0';
                else break;
            }
            if(count.length()==int(abs(len)))
            {
                printf("NO");
                return 0;
            }
        }
    printf("YES");
    return 0;
}

posted on 2016-07-08 17:56  解治  阅读(134)  评论(0)    收藏  举报