zoj3861——Valid Pattern Lock(全排列)

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.

valid_pattern_lock
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

脑子笨的我写不出dfs,只好用全排列做了。因为每个相邻的数字之间如果没有其他数,或者有其他数,但这个数之前就已经经过了。所以只要判断相邻的两个数之间有没有其他数,注意全排列时要先排序,因为系统的全排列函数只按照字典序

#include<iostream>
#include <cstdio>
#include <algorithm>
#include<cmath>
#include <cstring>
#define MAXN 105
#define inf 0x3f3f3f3f
using namespace std;
int map[15][15],a[15],vis[15],f[200010][15];
int main()
{
    memset(map,0,sizeof(map));
    map[1][7]=map[7][1]=4;
    map[1][3]=map[3][1]=2;
    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,j;
    scanf("%d",&t);
    while(t--)
    {
        int ans=0;
        memset(a,0,sizeof(a));
        memset(vis,0,sizeof(vis));
        scanf("%d",&n);
        for(int i=0; i<n; ++i)
        {
            scanf("%d",&a[i]);
        }
        bool flag=true;
        sort(a,a+n);
        for(int i=0; i<n-1; ++i)
        {
            memset(vis,0,sizeof(vis));
            j=i+1;

            vis[a[i]]=vis[a[j]]=1;
            if(map[a[i]][a[j]]&&(!vis[map[a[i]][a[j]]]))
            {
                flag=false;
                break;
            }

        }
        if(flag)
        {
            for(int i=0; i<n; ++i)
                f[ans][i]=a[i];
            ans++;
        }


        while(next_permutation(a,a+n))
        {
            memset(vis,0,sizeof(vis));

            flag=true;
            for(int i=0; i<n-1; ++i)
            {
                j=i+1;

                vis[a[i]]=vis[a[j]]=1;
                if(map[a[i]][a[j]]&&(!vis[map[a[i]][a[j]]]))
                {
                    flag=false;
                    break;
                }

            }
            if(flag)
            {
                for(int i=0; i<n; ++i)
                    f[ans][i]=a[i];
                ans++;
            }
        }
        printf("%d\n",ans);
        if(ans>0)
        {
            for(int i=0; i<ans; ++i)
            {
                for(j=0; j<n-1; ++j)
                    printf("%d ",f[i][j]);
                printf("%d\n",f[i][j]);
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值