码迷,mamicode.com
首页 > 其他好文 > 详细

2016 省热身赛 Valid Pattern Lock

时间:2016-05-03 00:16:13      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:

Valid Pattern Lock
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu
 

Description

Pattern lock security is generally used in Android handsets instead of a password. The pattern lock can be set by joining points on a 3 × 3 matrix in a chosen order. The points of the matrix are registered in a numbered order starting with 1 in the upper left corner and ending with 9 in the bottom right corner.

技术分享

A valid pattern has the following properties:

  • A pattern can be represented using the sequence of points which it‘s touching for the first time (in the same order of drawing the pattern). And we call those points as active points.
  • For every two consecutive points A and B in the pattern representation, if the line segment connecting A and B passes through some other points, these points must be in the sequence also and comes before A and B, otherwise the pattern will be invalid.
  • In the pattern representation we don‘t mention the same point more than once, even if the pattern will touch this point again through another valid segment, and each segment in the pattern must be going from a point to another point which the pattern didn‘t touch before and it might go through some points which already appeared in the pattern.

 

Now you are given n active points, you need to find the number of valid pattern locks formed from those active points.

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first line contains an integer n (3 ≤ n ≤ 9), indicating the number of active points. The second line contains n distinct integers a1, a2, … an (1 ≤ ai ≤ 9) which denotes the identifier of the active points.

Output

For each test case, print a line containing an integer m, indicating the number of valid pattern lock.

In the next m lines, each contains n integers, indicating an valid pattern lock sequence. The m sequences should be listed in lexicographical order.

Sample Input

1
3
1 2 3

Sample Output

4
1 2 3
2 1 3
2 3 1
3 2 1
被这道题目坑哭了快。。。
给出几个数字,判断几种手机设置锁的方式,并输出,,做起来很简单,,只是,,一开始因用cout超时,折磨很久,又因为没有
输出,一直WA 好苦。。。。
# include <iostream>
# include <cstdio>
# include <cstring>
using namespace std;
int pl[362882][10];
int num[362882][10];
int h = -1;
void Perm(int list[], int k, int m)
{

    if(k == m)
    {
        h++;
        for(int i = 0; i <= m; i++)
            pl[h][i] = list[i];
    }
    else
        for(int j = k; j <= m; j++)
        {
            swap(list[k], list[j]);
            Perm(list, k + 1, m);
            swap(list[k], list[j]);
        }
}
int map[20] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int is[20];

bool pan(int x, int y)
{
    if(x == 1)
    {
        if(y == 3 && is[2] == 0)
        return false;
        if(y == 7 && is[4] == 0)
        return false;
        if(y == 9 && is[5] == 0)
        return false;
    }
    if(x == 2)
    {
        if(y == 8 && is[5] == 0)
        return false;
    }
    if(x == 3)
    {
        if(y == 1 && is[2] == 0)
        return false;
        if(y == 7 && is[5] == 0)
        return false;
        if(y == 9 && is[6] == 0)
        return false;
    }
    if(x == 4)
    {
        if(y == 6 && is[5] == 0)
        return false;
    }
    if(x == 6)
    {
        if(y == 4 && is[5] == 0)
        return false;
    }
    if(x == 7)
    {
        if(y == 1 && is[4] == 0)
        return false;
        if(y == 9 && is[8] == 0)
        return false;
        if(y == 3 && is[5] == 0)
        return false;
    }
    if(x == 8)
    {
        if(y == 2 && is[5] == 0)
        return false;
    }

    if(x == 9)
    {
        if(y == 1 && is[5] == 0)
        return false;
        if(y == 3 && is[6] == 0)
        return false;
        if(y == 7 && is[8] == 0)
        return false;
    }
    return true;
}


bool f(int list[], int n)
{
    memset(is, 0, sizeof(is));
    for(int i = 0; i < n - 1; i++)
    {
        is[list[i]] = 1;
        // 判断到下一个是否可以
        if(!pan(list[i], list[i + 1]))
        return false;
    }
    return true;
}
int jie(int n)
{
    int sum = 1;
    while(n)
    {
        sum *= n;
        n--;
    }
    return sum;
}
int main()
{

    int t;
    scanf("%d", &t);
    while(t--)
    {
        memset(pl, 0, sizeof(pl));
        int a[12];
        int n;
        scanf("%d", &n);
        for(int i = 0; i < n; i++)
            scanf("%d", &a[i]);

        h = -1;
        Perm(a, 0, n - 1);
        int jiecheng = jie(n);
        int jishu = -1;

        for(int i = 0; i < jiecheng; i++)
        {
            if(f(pl[i], n)) // 符合条件
            {
                jishu++;
                for(int j = 0; j < n; j++)
                {
                    num[jishu][j] = pl[i][j];
                    //if(j == 0)
                    //cout << pl[i][j];
                    //else
                    //cout << " " << pl[i][j];
                }
                //cout << endl;
            }
        }
        printf("%d\n", jishu + 1);//cout << jishu + 1 << endl;
        for(int i = 0; i < jishu + 1; i++)
        {
            for(int j = 0; j < n; j++)
            {
                if(j == 0)
                printf("%d", num[i][j]);//cout << num[i][j];
                else
                printf(" %d", num[i][j]);//cout << " " << num[i][j];
            }
            printf("\n");//cout << endl;
        }
    }

    return 0;
}

 



2016 省热身赛 Valid Pattern Lock

标签:

原文地址:http://www.cnblogs.com/lyf-acm/p/5453503.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!