A. Mike and Cellphone
题意:
给定一个0-9数字键盘,随后输入一个操作序列,
问该操作序列在键盘上形成的手势是否是唯一的,是YES,否NO。
计算起点与其他点位置之间的差额(方向队列记录),从头开始,直接寻找,模拟过程;
AC代码:
#include <cstdio>
#include <iostream>
#include<algorithm>
#include<string>
#include<queue>
struct node
{
int x;
int y;
};
using namespace std;
int ans[6][5] = {-1,-1,-1,-1,-1,-1 ,1, 2, 3,-1,-1, 4, 5, 6,-1,-1, 7, 8, 9,-1,-1,-1, 0,-1,-1,-1,-1,-1,-1,-1};
int main()
{
int n;
node q[10];
q[0].x = 4; q[0].y = 2;
q[1].x = 1; q[1].y = 1;
q[2].x = 1; q[2].y = 2;
q[3].x = 1; q[3].y = 3;
q[4].x = 2; q[4].y = 1;
q[5].x = 2; q[5].y = 2;
q[6].x = 2; q[6].y = 3;
q[7].x = 3; q[7].y = 1;
q[8].x = 3; q[8].y = 2;
q[9].x = 3; q[9].y = 3;
while (scanf("%d",&n)!=EOF)
{
node b;
queue<node>dir;
char a[100];
int k;
scanf("%s", a);
for (int i = 0; i < n; i++)
{
int x = a[i] - '0';
if (i == 0) { b = q[x]; k = x; }
else
{
node task;
task.x = q[x].x - b.x;
task.y = q[x].y - b.y;
dir.push(task);
}
}
int count;
for (int i = 0; i <= 10; i++)
{
if (k == i) continue;
queue<node>f = dir;
count = 0;
while (!f.empty())
{
node task = f.front(); f.pop();
int xx = q[i].x + task.x; int yy = q[i].y + task.y;
if (xx >= 1 && xx <= 4 && yy >= 1 && yy<=3 && ans[xx][yy]!=-1 )
{
count++;
}
if (count == n - 1) break;
}
if (count == n - 1) break;
}
if (count == n - 1) printf("NO\n");
else printf("YES\n");
}
return 0;
}