hdu-5641 King's Phone (水题)
Posted LittlePointer
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu-5641 King's Phone (水题)相关的知识,希望对你有一定的参考价值。
题目链接:
King‘s Phone
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 418 Accepted Submission(s): 123
Problem Description
In a military parade, the King sees lots of new things, including an Andriod Phone. He becomes interested in the pattern lock screen.
The pattern interface is a 3×3 square lattice, the three points in the first line are labeled as 1,2,3, the three points in the second line are labeled as 4,5,6, and the three points in the last line are labeled as 7,8,9。The password itself is a sequence, representing the points in chronological sequence, but you should follow the following rules:
- The password contains at least four points.
- Once a point has been passed through. It can‘t be passed through again.
- The middle point on the path can‘t be skipped, unless it has been passed through(3427 is valid, but 3724 is invalid).
His password has a length for a positive integer k(1≤k≤9), the password sequence is s1,s2...sk(0≤si<INT_MAX) , he wants to know whether the password is valid. Then the King throws the problem to you.
The pattern interface is a 3×3 square lattice, the three points in the first line are labeled as 1,2,3, the three points in the second line are labeled as 4,5,6, and the three points in the last line are labeled as 7,8,9。The password itself is a sequence, representing the points in chronological sequence, but you should follow the following rules:
- The password contains at least four points.
- Once a point has been passed through. It can‘t be passed through again.
- The middle point on the path can‘t be skipped, unless it has been passed through(3427 is valid, but 3724 is invalid).
His password has a length for a positive integer k(1≤k≤9), the password sequence is s1,s2...sk(0≤si<INT_MAX) , he wants to know whether the password is valid. Then the King throws the problem to you.
Input
The first line contains a number T(0<T≤100000), the number of the testcases.
For each test case, there are only one line. the first first number k,represent the length of the password, then k numbers, separated by a space, representing the password sequence s1,s2...sk.
For each test case, there are only one line. the first first number k,represent the length of the password, then k numbers, separated by a space, representing the password sequence s1,s2...sk.
Output
Output exactly T lines. For each test case, print `valid` if the password is valid, otherwise print `invalid`
Sample Input
3
4 1 3 6 2
4 6 2 1 3
4 8 1 6 7
Sample Output
invalid
valid
valid
AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int k,a[12],vis[12]; int check1() { if(k<4)return 0; memset(vis,0,sizeof(vis)); for(int i=1;i<=k;i++) { if(a[i]>9||a[i]<1||vis[a[i]]==1) { return 0; } else { vis[a[i]]=1; } } return 1; } int check2() { memset(vis,0,sizeof(vis)); for(int i=1;i<k;i++) { if(a[i]==1&&a[i+1]==3&&vis[2]==0)return 0; if(a[i]==3&&a[i+1]==1&&vis[2]==0)return 0; if(a[i]==1&&a[i+1]==7&&vis[4]==0)return 0; if(a[i]==7&&a[i+1]==1&&vis[4]==0)return 0; if(a[i]==1&&a[i+1]==9&&vis[5]==0)return 0; if(a[i]==9&&a[i+1]==1&&vis[5]==0)return 0; if(a[i]==3&&a[i+1]==9&&vis[6]==0)return 0; if(a[i]==9&&a[i+1]==3&&vis[6]==0)return 0; if(a[i]==7&&a[i+1]==3&&vis[5]==0)return 0; if(a[i]==3&&a[i+1]==7&&vis[5]==0)return 0; if(a[i]==7&&a[i+1]==9&&vis[8]==0)return 0; if(a[i]==9&&a[i+1]==7&&vis[8]==0)return 0; if(a[i]==4&&a[i+1]==6&&vis[5]==0)return 0; if(a[i]==6&&a[i+1]==4&&vis[5]==0)return 0; if(a[i]==2&&a[i+1]==8&&vis[5]==0)return 0; if(a[i]==8&&a[i+1]==2&&vis[5]==0)return 0; vis[a[i]]=1; } return 1; } int main() { int t; scanf("%d",&t); while(t--) { scanf("%d",&k); for(int i=1;i<=k;i++) { scanf("%d",&a[i]); } if(check1()==0||check2()==0)cout<<"invalid"<<"\n"; else cout<<"valid"<<"\n"; } return 0; }
以上是关于hdu-5641 King's Phone (水题)的主要内容,如果未能解决你的问题,请参考以下文章