B - Mike and Cellphone(map)

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

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"。做法:将该记忆路径向四个方向(上下左右)各移动一格,如果都超出老式键盘的范围,说明记忆手势产生的密码唯一,则输出"YES",否则输出"NO",具体注解看代码,一遍简单过。

AC代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 /*tmp[6][5]:
 4    0  1  2  3  4
 5 0 -1 -1 -1 -1 -1
 6 1 -1  1  2  3 -1
 7 2 -1  4  5  6 -1
 8 3 -1  7  8  9 -1
 9 4 -1 -1  0 -1 -1
10 5 -1 -1 -1 -1 -1
11 */
12 int main(){
13     int n,tmp[6][5],cnt=1,num=0,dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};//方向数组:上右下左
14     char s[10];bool flag;
15     memset(tmp,-1,sizeof(tmp));tmp[4][2]=0;
16     map<char,pair<int,int> > mp;//键值对,表示键盘中数字对应的坐标(first,second)
17     mp['1'].first=1,mp['1'].second=1;
18     mp['2'].first=1,mp['2'].second=2;
19     mp['3'].first=1,mp['3'].second=3;
20     mp['4'].first=2,mp['4'].second=1;
21     mp['5'].first=2,mp['5'].second=2;
22     mp['6'].first=2,mp['6'].second=3;
23     mp['7'].first=3,mp['7'].second=1;
24     mp['8'].first=3,mp['8'].second=2;
25     mp['9'].first=3,mp['9'].second=3;
26     mp['0'].first=4,mp['0'].second=2;
27     for(int i=1;i<4;++i)//tmp数组初始化
28         for(int j=1;j<4;++j)
29             tmp[i][j]=cnt++;
30     cin>>n;getchar();//吃掉回车符对字符串的影响
31     cin>>s;
32     for(int x=0;x<4;++x){//枚举四个方向
33         flag=false;
34         for(int j=0;j<n;++j)
35             if(tmp[mp[s[j]].first+dir[x][0]][mp[s[j]].second+dir[x][1]]<0){flag=true;break;}
36         if(flag){num++;}//只要小于0,即超出老式键盘的范围,计数器就加1
37     }
38     if(num==4)cout<<"YES"<<endl;//只要向4个方向移动一格后都超出老式键盘的范围,说明记忆手势产生唯一的按键密码,则该密码正确
39     else cout<<"NO"<<endl;//否则还有其他不确定的密码,则为NO
40     return 0;
41 }

 

posted @ 2018-06-06 21:20  霜雪千年  阅读(223)  评论(0编辑  收藏  举报