ZOJ 3861 Valid Pattern Lock

来源:互联网 发布:怎么做网络推广赚钱 编辑:程序博客网 时间:2024/04/30 19:15

Valid Pattern Lock

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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 containsn 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. Them sequences should be listed in lexicographical order.

Sample Input

131 2 3

Sample Output

41 2 32 1 32 3 13 2 1
题意:判断可以绘制的手机锁有几种情况。
很不服,
竟然没搞出来。。
说白了就是规则没弄清楚。
其实只要开个数组先记录所有出现的数,然后一个数能不能经过只要判断靠它隔两个数用过就好了。只要隔两个数用过,它旁边的那个数就能经过,,妈的。
一个水DFS
#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<iostream>#include<algorithm>using namespace std;int n,a[15];int ans[15];bool ca[15];bool used[15];int cnt;bool judge(int cur,int last){    if(cur==1)    {        if(last==7 && (!ca[4] || !used[4]))return 0;        if(last==3 && (!ca[2] || !used[2]))return 0;        if(last==9 && (!ca[5] || !used[5]))return 0;    }    if(cur==2)    {        if(last==8 && (!ca[5] || !used[5]))return 0;    }    if(cur==3)    {        if(last==1 && (!ca[2] || !used[2]))return 0;        if(last==7 && (!ca[5] || !used[5]))return 0;        if(last==9 && (!ca[6] || !used[6]))return 0;    }    if(cur==4)    {        if(last==6 && (!ca[5] || !used[5]))return 0;    }    if(cur==6)    {        if(last==4 && (!ca[5] || !used[5]))return 0;    }    if(cur==7)    {        if(last==1 && (!ca[4] || !used[4]))return 0;        if(last==3 && (!ca[5] || !used[5]))return 0;        if(last==9 && (!ca[8] || !used[8]))return 0;    }    if(cur==8)    {        if(last==2 && (!ca[5] || !used[5]))return 0;    }    if(cur==9)    {        if(last==7 && (!ca[8] || !used[8]))return 0;        if(last==3 && (!ca[6] || !used[6]))return 0;        if(last==1 && (!ca[5] || !used[5]))return 0;    }    return 1;}void dfs_cnt(int dep,int last){    if(dep==n)    {        cnt++;    }    for(int i=1;i<=n;i++)    {        if(!used[a[i]] && judge(a[i],last))        {            used[a[i]]=1;            ans[dep]=a[i];            dfs_cnt(dep+1,a[i]);            used[a[i]]=0;        }    }}void dfs_print(int dep,int last){    if(dep==n)    {        for(int i=0;i<n;i++)        {            if(!i)printf("%d",ans[i]);            else printf(" %d",ans[i]);        }        printf("\n");    }    for(int i=1;i<=n;i++)    {        if(!used[a[i]] && judge(a[i],last))        {            used[a[i]]=1;            ans[dep]=a[i];            dfs_print(dep+1,a[i]);            used[a[i]]=0;        }    }}int main(){    int T;    scanf("%d",&T);    while(T--)    {        scanf("%d",&n);        for(int i=1;i<=9;i++)        {            used[i]=1;            ca[i]=0;        }        for(int i=1;i<=n;i++)        {            scanf("%d",&a[i]);            used[a[i]]=0;            ca[a[i]]=1;        }        sort(a+1,a+n+1);        cnt=0;        dfs_cnt(0,0);        printf("%d\n",cnt);        dfs_print(0,0);    }    return 0;}


0 0
原创粉丝点击