King's Phone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 74 Accepted Submission(s): 23
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 hint: For test case #1:The path $1\rightarrow 3$ skipped the middle point $2$, so it's invalid. For test case #2:The path $1\rightarrow 3$ doesn't skipped the middle point $2$, because the point 2 has been through, so it's valid. For test case #2:The path $8\rightarrow 1 \rightarrow 6 \rightarrow 7$ doesn't have any the middle point $2$, so it's valid.
考虑要全面。。。。
AC-code:
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
int T,n,i,k,a,flag,vis[10],m[10];
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
flag=0;
for(i=0;i<n;i++)
{
scanf("%d",&m[i]);
if(m[i]<1||m[i]>9)
flag=1;
}
if(n<4||flag)
{
printf("invalid\n");
continue;
}
memset(vis,0,sizeof(vis));
vis[m[0]]=1;
for(i=0;i<n-1;i++)
{
k=m[i];
a=m[i+1];
if(vis[a])
break;
vis[k]=vis[a]=1;
if(k-a==2&&(k==3||k==6||k==9))
{
if(!vis[k-1])
break;
}
else if(a-k==2&&(a==3||a==6||a==9))
{
if(!vis[a-1])
break;
}
else if(a-k==6&&(a==7||a==8||a==9))
{
if(!vis[a-3])
break;
}
else if(k-a==6&&(k==7||k==8||k==9))
{
if(!vis[k-3])
break;
}
else if(a+k==10)
if(!vis[5])
break;
else if((a==0&&k==5)||(a==5&&k==0))
if(!vis[8])
break;
else if((a==0&&k==2)||(a==2&&k==0))
if(!vis[8]||!vis[5])
break;
}
if(i==n-1)
printf("valid\n");
else
printf("invalid\n");
}
return 0;
}