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?
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.
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.
3 586
NO
2 09
NO
9 123456789
YES
3 911
YES
You can find the picture clarifying the first sample case in the statement above.
主要考虑最下端的0,看其是否可以上移或平移
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 1000000007
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=5e2+10;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
int book[100][100];
int main()
{
int maxx=-1,maxy=-1;
int minx= 10000,miny = 10000;
int n;
cin >>n;
string s;
cin >>s;
for(int i=0;i<n;i++)
{
int t = s[i]-'0';
int x = (t-1)/3+1;
int y = (t)%3;
if(y==0)
y = 3;
if(t==0)
{
x = 4;
y =2 ;
}
book[x][y]=1;
if(maxx<x)
maxx=x ;
if(maxy<y)
maxy=y;
if(minx>x)
minx=x;
if(miny>y)
miny = y;
}
//cout << maxx<<maxy<<minx<<miny<<endl;
if(maxx-minx==3)
{
cout <<"YES"<<endl;
return 0;
}
if(maxx-minx >= 2&&maxy-miny>=2&&!(book[3][1]==0&&book[3][2]==1&&book[3][3]==0&&book[4][2]==0)&&book[4][2]==0)
{
cout << "YES"<<endl;
return 0;
}
cout << "NO"<<endl;
return 0;
}