zoj 3861 Valid Pattern Lock(dfs)

本文介绍了一种在Android设备上使用的图案解锁算法的有效图案锁问题。该算法通过指定的活动点来确定有效的解锁图案,并详细解释了有效图案的定义及其实现方式。文章最后提供了一个C++实现的示例代码,用于找出所有符合条件的有效图案锁序列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 contains n distinct integers a1a2, … 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 <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 500010
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

int Read()
{
    char ch;
    int a = 0;
    while((ch = getchar()) == ' ' | ch == '\n');
    a += ch - '0';
    while((ch = getchar()) != ' ' && ch != '\n')
    {
        a *= 10;
        a += ch - '0';
    }
    return a;
}

void Print(int a)    //Êä³öÍâ¹Ò
{
     if(a>9)
         Print(a/10);
     putchar(a%10+'0');
}
int n;
int mp[15][15];
int vis[15],ex[15];//分别表示 是否已经经过这个数字 这个数字是否给出
int num;
int ans[MAXN][15];
int res[15];

void init()
{
    MEM(mp,0);
    mp[1][3]=mp[3][1]=2;
    mp[4][6]=mp[6][4]=5;
    mp[7][9]=mp[9][7]=8;
    mp[1][7]=mp[7][1]=4;
    mp[2][8]=mp[8][2]=5;
    mp[3][9]=mp[9][3]=6;
    mp[1][9]=mp[9][1]=5;
    mp[3][7]=mp[7][3]=5;
}

void dfs(int cnt)
{
    if(cnt>n)
    {
        for(int i=1;i<=n;i++)
            ans[num][i]=res[i];
        num++;
//        return;
    }
    else
    {
        for(int i=1;i<=9;i++)
        {
            if((vis[i]==1)&&(ex[i]==1)&&(vis[mp[i][res[cnt-1]]]==0)&&(ex[mp[i][res[cnt-1]]]==1))
            {
                vis[i]=0;
                res[cnt]=i;
                dfs(cnt+1);
                vis[i]=1;
                res[cnt]=0;
            }
        }
    }
}

int main()
{
//    fread;
    init();
    int tc;
    scanf("%d",&tc);
    while(tc--)
    {
        scanf("%d",&n);
        MEM(vis,0); MEM(ex,0);
        ex[0]=1;
        for(int i=0;i<n;i++)
        {
            int x;
            scanf("%d",&x);
            vis[x]=ex[x]=1;
        }
        num=0;
//        res[0]=0;
        dfs(1);
        printf("%d\n",num);
        for(int i=0;i<num;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(j>1) printf(" ");
                printf("%d",ans[i][j]);
            }
            printf("\n");
        }
    }
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值