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.
题目大意是有一个人忘记了他的手机锁屏密码,只记得他解锁的手势的先后顺序,问这个解锁顺序是不是唯一的,是唯一的输出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;
}
本文介绍了一种通过模拟手机键盘并分析输入手势来判断解锁序列是否唯一的方法。该方法通过对输入的手势进行四个基本方向的平移验证,确定是否存在其他可能的解锁序列。
1352

被折叠的 条评论
为什么被折叠?



