您的位置:首页 > 其它

zoj 3861 Valid Pattern Lock (DFS)

2015-04-12 19:04 411 查看
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

图案锁取代密码锁广泛地应用于安卓手机。图案锁可以通过按你自己选的顺序连接3*3矩阵上的点来设置。

points of the matrix are registered in
a numbered order starting with 1 in theupper left cornerand ending with 9 in thebottom
right corner.

矩阵上的点按编号顺序被标记,从左上角的1开始,到右下角的9结束



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
在图案描绘的过程中每两个连续的点A和B,如果连接A和B的线段经过某些其他点,
must be in the sequence also and comes before A and B, otherwise the pattern will be invalid.
那么那些点必须在AB连线之前就在序列之中,否则该图案无效。
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
并且在图案中的每条线段必须从一个之前没画过的点开始到另一个[b]之前没画过的点结束,此线段可能经过[/b]
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.

现在给你n 个活跃点,求由有效点组成的有效图案锁的个数

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 integersa1,
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 inlexicographical order.(字典序)

Sample Input

1
3
1 2 3

Sample Output

4
1 2 3
2 1 3
2 3 1
3 2 1




1.pattern翻译为 图案

2.题意:给一些点,求用这些点组成有效的图案的个数

3.注意:比入7到9,那么8必须已走过,也就是说,8是不是活跃点或8没走过,那么就不能从7直接到9;

下次在写DFS时,可以写  满足条件时,再进行递归,不然像这次 有一坨的continue,而且容易出错

4.代码:

<pre name="code" class="cpp">#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int vis[15];
int a[15];
int path[15];
int pa[500000][15];
int n;
int cnt;

void DFS(int x,int layer)
{
    vis[x]=2;
    path[layer]=x;
    if(layer>=n)
    {
        cnt++;
        for(int i=1;i<=n;i++)
            pa[cnt][i]=path[i];
        return;
    }
    for(int i=1;i<=9;i++)
    {
        int tx=i;
        if(vis[i]==0||vis[i]==2)
            continue;
        if(x==1&&tx==3&&(vis[2]==1||vis[2]==0))
            continue;
        if(x==1&&tx==7&&(vis[4]==1||vis[4]==0))
            continue;
        if(x==1&&tx==9&&(vis[5]==1||vis[5]==0))
            continue;
        if(x==2&&tx==8&&(vis[5]==1||vis[5]==0))
            continue;
        if(x==3&&tx==1&&(vis[2]==1||vis[2]==0))
            continue;
        if(x==3&&tx==9&&(vis[6]==1||vis[6]==0))
            continue;
        if(x==3&&tx==7&&(vis[5]==1||vis[5]==0))
            continue;
        if(x==4&&tx==6&&(vis[5]==1||vis[5]==0))
            continue;
        if(x==6&&tx==4&&(vis[5]==1||vis[5]==0))
            continue;
        if(x==7&&tx==1&&(vis[4]==1||vis[4]==0))
            continue;
        if(x==7&&tx==9&&(vis[8]==1||vis[8]==0))
            continue;
        if(x==7&&tx==3&&(vis[5]==1||vis[5]==0))
            continue;
        if(x==8&&tx==2&&(vis[5]==1||vis[5]==0))
            continue;
        if(x==9&&tx==7&&(vis[8]==1||vis[8]==0))
            continue;
        if(x==9&&tx==3&&(vis[6]==1||vis[6]==0))
            continue;
        if(x==9&&tx==1&&(vis[5]==1||vis[5]==0))
            continue;
        layer=layer+1;
        DFS(tx,layer);
        layer--;
        vis[tx]=1;
    }
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        cnt=0;
        memset(vis,0,sizeof(vis));
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            vis[a[i]]=1;
        }
        sort(a,a+n);
        for(int i=0;i<n;i++)
        {
            DFS(a[i],1);
            vis[a[i]]=1;
        }
        printf("%d\n",cnt);
        for(int i=1;i<=cnt;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(j==n)
                    printf("%d\n",pa[i][j]);
                else
                    printf("%d ",pa[i][j]);
            }
        }
    }
    return 0;
}




                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: