HDU5641(打表,模拟)

题目:

King’s Phone

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2894 Accepted Submission(s): 659

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

1 2 3
4 5 6
7 8 9

意思是,走1、3,那么2必须先走(横),走1、7那么4必须先走(竖),走1、9那么5必须先走(对角线),原来这个中点的意思是这样,exo me???

弄清楚题意后,就很简单了,用一个二维数组记录走这两个点必走的点是什么,注意其他没有用到的二维数组的元素为0,再用一个vis数组记录这个点是否走过。

坑点:
输入要判断k的取值范围,判断每一个S[i]是否大于等于1小于等于9
注意如果想离线处理每一个数,那么如果遇到invalid的情况就break或者continue的话,缓冲区中会有数据残留。

#include <iostream>
#include <algorithm>
#include <cstring>
int arr[10];
int map[15][15];
int visited[10];

using namespace std;

int main(){
    memset(map, 0, sizeof(map));
    map[1][3] = map[3][1] = 2;
    map[1][7] = map[7][1] = 4;
    map[1][9] = map[9][1] = 5;
    map[2][8] = map[8][2] = 5;
    map[3][7] = map[7][3] = 5;
    map[3][9] = map[9][3] = 6;
    map[4][6] = map[6][4] = 5;
    map[7][9] = map[9][7] = 8;
    int t, n;
    cin>>t;
    while(t--){
        cin>>n;
        int flag = 1;
        memset(visited, 0, sizeof(visited));
        memset(arr, 0, sizeof(arr));
        for(int i=0; i<n; i++){
            cin>>arr[i];
            if(arr[i]>9||arr[i]<1)
                flag = -1;
        }

        if(n>9||n<4||flag==-1){
            cout<<"invalid"<<endl;
            continue;
        }

        visited[arr[0]] = 1;

        for(int i=1; i<n; i++){
            if(visited[arr[i]]==1){
                flag = -1;
                break;
            }
            else if(map[arr[i]][arr[i-1]]==0||visited[map[arr[i]][arr[i-1]]])
                visited[arr[i]] = 1;
            else
                flag = -1;
        }
        if(flag==-1) cout<<"invalid"<<endl;
        else cout<<"valid"<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值