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&nbsp;T(0<T≤100000), the number of the testcases.

For each test case, there are only one line. the first first number&nbsp;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

BestCoder Round #75

一个简单的模拟题,首先判断序列长度是否合法,接着判断 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(暴力模拟题)

时间: 2024-08-02 07:01:23

hdu 5641 King's Phone(暴力模拟题)的相关文章

hdu 5640 King&#39;s Cake(模拟)

Problem Description It is the king's birthday before the military parade . The ministers prepared a rectangle cake of size n×m(1≤n,m≤10000) . The king plans to cut the cake himself. But he has a strange habit of cutting cakes. Each time, he will cut

HDU 1081 To The Max 暴力模拟O(n^4) dp优化O(n^3)

原题: http://acm.hdu.edu.cn/showproblem.php?pid=1081 题目大意: 求给定边长的正方形选一个矩形,使它包含的所有元素的值最大. 大家都知道(a+b)^2的展开式,这里的优化就是用了这个原理来做的优化,我们的dp值是我们前i行j列的矩形区域的值. 任意矩形区域的值通过该展开式也能求解,所以我们可以暴力枚举每种以左上角(k,l)到右下角(i,j)的情况. 对于这个题边长是100,4层循环是10^8,因为循环并跑不了这么多,刚好也能卡过去. 代码如下: #

HDU 5059 Help him(简单模拟题)

http://acm.hdu.edu.cn/showproblem.php?pid=5059 题目大意: 给定一个字符串,如果这个字符串是一个整数,并且这个整数在[a,b]的范围之内(包括a,b),那就输出YES,其它的都是NO. 这个字符串是整数的条件: 1.如果它是正整数,它只包含前导不是0的数(这个数前面没有零). 2.如果它是负整数,只包含一个'-'符号,任然没有前导0. 3.除此之外都不是非法的 解题思路: http://bestcoder.hdu.edu.cn/ 这里有 要注意: 0

hdu(5402)——Travelling Salesman Problem(模拟题)

啊...这道题我一开始的想法是dp,因为我们要求的是在这个区间中和的最大值. 但是没想到只要暴力就好了. 这道题用到了一个著名的想法是:黑白棋盘染色问题. 题意: 现在给你一个n*m的矩阵,然后告诉你每个矩阵中的数字,然后现在要从左上角走到右下角,然后问你所能获得的数字和的最大值是多少.当然,你只能往四个方向走,而且每个点只能走一次.并且叫你输出路径. 思路: 这里我分了三种情况. 1)首先当行或者列数都是1的时候,那么我们就只可能有一种走法(横着走或者是竖着走)然后获取所有的数值. 2)当行数

【ZZNU-oj-2116:人间不值得】(1亿以内的货币拼音转数值求折扣价原价)--hash+String大法好+字符串处理+超大暴力模拟题

B : 人间不值得 概览问题列表状态排名编辑 Progress Bar 时间限制:1 Sec 内存限制:256 MiB提交:146 答案正确:12 提交 编辑 题目描述 家缠万贯来几时,我今停杯一问之.人攀暴富不可得,贫穷却与人相随.何以解忧,唯有暴富.spring做梦都想暴富,但是又苦于囊中羞涩.每次消费都会精打细算,所以每次消费都会记在小本本上.可惜spring是个汉字盲,你能帮他来完成汉字拼音转化为数字么.spring每次的消费都是一行格式表示分别为原价(或折扣价) 价格   折扣度  

HDU ACM 1035 Robot Motion 简单模拟题

分析:一步步的走,走出矩阵则说明没有环,若走到已经走过的地方,说明有环,按格式输出结果,OK. #include<iostream> using namespace std; #define N 15 int dir[4][2]={-1,0,1,0,0,-1,0,1}; char map[N][N]; int vis[N][N]; char ch[]="NSWE"; int n,m; int id(char c) { int i; for(i=0;i<4;i++) i

HDU 4561 连续最大积 (模拟题)

#include <iostream> #include<cstdio> using namespace std; int a[50000+100],n; int solve(int s,int e) { int i,cnt=0,st,ed,flag=1,ans=0; //if(s==n+1) return 0; for(i=s;i<e;i++) { if(a[i]==-2) { if(flag) { flag=0; st=i;//记录此区间内-2第一次出现的位置 } ed=

2017/7/31-zznu-oj-问题 B: N! 普拉斯 -【求大数的阶乘-ll存不下-然后取尾零的个数输出-暴力模拟】

问题 B: N! 普拉斯 时间限制: 1 Sec  内存限制: 128 MB提交: 114  解决: 35[提交] [状态] [讨论版] [命题人:admin] 题目描述 在处理阶乘时也需要借助计算器. 在计算机中,数字是通过像01像素矩阵来显示的,最终的显示效果如下:  宝儿姐一直在思考一个问题,N!末尾究竟有多少个0?我们假设N!末尾有k个0,请按照规则打印k. 输入 输入一个正整数n(n< 50) ,输入以EOF结尾. 输出 我们假设N!末尾有k个0,请按照规则打印k,数字之间间隔3列0.

HDU 4028 The time of a day STL 模拟题

暴力出奇迹.. #include<stdio.h> #include<iostream> #include<algorithm> #include<vector> #include<cmath> #include<queue> #include<set> #include<map> using namespace std; #define ll __int64 #define N 42 ll n,m,ans;