题意:给定一个手机键盘数字九宫格,然后让你判断某种操作是不是唯一的,也就是说是不是可以通过平移也能实现。

析:我的想法是那就平移一下,看看能实现,就四种平移,上,下,左,右,上是-3,要注意0变成8,如果有数字变成小于等于0了,那么就是不可以,同理,下是+3,8可以变成0,其他的也是这样,

注意左右平移是147,和369,是不能平移,然后就AC了。再简化一下就是如果有123,就不能上移,如果有79就不能下移,如果有147就不能左移,如果有369就不能右移,如果有0就不能下左右移。

代码如下:

#include <iostream>
#include <cstdio> using namespace std;
const int maxn = 9 + 5;
int a[maxn];
int b[maxn];
char s[maxn]; int main(){
int n;
while(scanf("%d", &n) == 1 && n){
scanf("%s", s);
for(int i = 0; i < n; ++i) a[i] = s[i] - '0';
bool ok = false;
for(int i = 0; i < n; ++i)//下
if(a[i] && a[i] != 8) b[i] = a[i] + 3;
else if(a[i] == 8) b[i] = 0;
else b[i] = 10;
int i;
for(i = 0; i < n; ++i) if(b[i] > 9) break;
if(i == n) ok = true;
for(i = 0; i < n; ++i)//上
if(a[i]) b[i] = a[i] - 3;
else b[i] = 8;
for(i = 0; i < n; ++i) if(b[i] <= 0) break;
if(i == n) ok = true;
for(i = 0; i < n; ++i)//右
if(3 == a[i] || 6 == a[i] || 9 == a[i]) b[i] = 10;
else if(a[i]) b[i] = a[i] + 1;
else b[i] = 10;
for(i = 0; i < n; ++i) if(b[i] > 9) break;
if(i == n) ok = true;
for(i = 0; i < n; ++i)//左
if(a[i] == 1 || a[i] == 4 || a[i] == 7) b[i] = -10;
else if(a[i]) b[i] = a[i] - 1;
else b[i] = -10;
for(i = 0; i < n; ++i) if(b[i] <= 0) break;
if(i == n) ok = true;
if(!ok) puts("YES");
else puts("NO");
}
return 0;
}

第二种:

#include <iostream>
#include <cstdio> using namespace std;
const int maxn = 9 + 5;
int a[maxn];
int b[maxn];
char s[maxn]; int main(){
int n;
while(scanf("%d", &n) == 1 && n){
scanf("%s", s);
int l = 0, u = 0, r = 0, d = 0;
for(int i = 0; i < n; ++i){
if(s[i] == '0') l = r = d = 1;
if(s[i] == '1' || s[i] == '4' || s[i] == '7') l = 1;
if(s[i] == '3' || s[i] == '6' || s[i] == '9') r = 1;
if(s[i] == '1' || s[i] == '2' || s[i] == '3') u = 1;
if(s[i] == '7' || s[i] == '9') d = 1;
}
if(u && d && l && r) puts("YES");
else puts("NO");
}
return 0;
}
05-11 20:01