hdu 5641 King's Phone(暴力模拟题)
Posted UniqueColor
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu 5641 King's Phone(暴力模拟题)相关的知识,希望对你有一定的参考价值。
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.
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.
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.
Source
一个简单的模拟题,首先判断序列长度是否合法,接着判断 sis_is?i?? 是否在 [1,9],最后扫一遍看看有没有重复以及跨过中间点的情况即可。
复杂度:O(nT)。
AC代码:
1 #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include<iostream> 3 #include<cstdio> 4 #include<cstring> 5 #include<cmath> 6 #include<math.h> 7 #include<algorithm> 8 #include<queue> 9 #include<set> 10 #include<bitset> 11 #include<map> 12 #include<vector> 13 #include<stdlib.h> 14 #include <stack> 15 using namespace std; 16 #define PI acos(-1.0) 17 #define max(a,b) (a) > (b) ? (a) : (b) 18 #define min(a,b) (a) < (b) ? (a) : (b) 19 #define ll long long 20 #define eps 1e-10 21 #define MOD 1000000007 22 #define N 16 23 #define inf 1e12 24 int n; 25 int a[N],vis[N]; 26 bool judge(int num1,int num2){ 27 if(vis[num2]) return false; 28 vis[num2]=1; 29 if(num1==1){ 30 if(num2==3 && vis[2]==0){ 31 return false; 32 } 33 if(num2==7 && vis[4]==0){ 34 return false; 35 } 36 if(num2==9 && vis[5]==0){ 37 return false; 38 } 39 } 40 if(num1==2){ 41 if(num2==8 && vis[5]==0){ 42 return false; 43 } 44 } 45 if(num1==3){ 46 if(num2==7 && vis[5]==0){ 47 return false; 48 } 49 if(num2==1 && vis[2]==0){ 50 return false; 51 } 52 if(num2==9 && vis[6]==0){ 53 return false; 54 } 55 } 56 if(num1==4){ 57 if(num2==6 && vis[5]==0){ 58 return false; 59 } 60 } 61 if(num1==6){ 62 if(num2==4 && vis[5]==0){ 63 return false; 64 } 65 } 66 if(num1==7){ 67 if(num2==1 && vis[4]==0){ 68 return false; 69 } 70 if(num2==3 && vis[5]==0){ 71 return false; 72 } 73 if(num2==9 && vis[8]==0){ 74 return false; 75 } 76 } 77 if(num1==8){ 78 if(num2==2 && vis[5]==0){ 79 return false; 80 } 81 } 82 if(num1==9){ 83 if(num2==1 && vis[5]==0){ 84 return false; 85 } 86 if(num2==3 && vis[6]==0){ 87 return false; 88 } 89 if(num2==7 && vis[8]==0){ 90 return false; 91 } 92 } 93 94 return true; 95 } 96 int main() 97 { 98 int t; 99 scanf("%d",&t); 100 while(t--){ 101 memset(vis,0,sizeof(vis)); 102 scanf("%d",&n); 103 int flag=1; 104 //int num_valid=0; 105 for(int i=0;i<n;i++){ 106 scanf("%d",&a[i]); 107 if(a[i]<1 || a[i]>9) flag=0; 108 } 109 if(flag==0){ 110 printf("invalid\n"); 111 continue; 112 } 113 flag=1; 114 for(int i=0;i<n;i++){ 115 if(vis[a[i]]){ 116 flag=0; 117 break; 118 } 119 vis[a[i]]=1; 120 } 121 if(flag==0) { 122 printf("invalid\n"); 123 continue; 124 } 125 if(n<4) { 126 printf("invalid\n"); 127 continue; 128 } 129 memset(vis,0,sizeof(vis)); 130 vis[a[0]]=1; 131 int ok=1; 132 for(int i=0;i<n-1;i++){ 133 if(judge(a[i],a[i+1])==false){ 134 ok=0; 135 break; 136 } 137 } 138 if(ok) printf("valid\n"); 139 else printf("invalid\n"); 140 } 141 return 0; 142 }
以上是关于hdu 5641 King's Phone(暴力模拟题)的主要内容,如果未能解决你的问题,请参考以下文章