• 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(1k9), the password sequence is s1,s2...sk(0si<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<T100000), 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
    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"<<"
    ";
            else cout<<"valid"<<"
    ";
        }
        return 0;
    }
  • 相关阅读:
    最长公共子序列算法问题代码(使用JavaScript实现)
    硬币找零问题算法几种不同的代码实现方式(使用Python实现)
    基数排序(使用Python实现)
    桶排序(使用Python实现)
    减法要用 signed 型
    16系列和18系列的不同
    Proteus中常用元器件名字
    数值类型
    PIC单片机之时钟设置
    MOS管使PIC单片机不能正常运行
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5270835.html
Copyright © 2020-2023  润新知