3861 - 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 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

#include <iostream>
#include <cmath>
#include <stdio.h>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <iomanip>
#include <algorithm>
#include <memory.h>
using namespace std;
int n;
int a[10];
int may[10];
bool vis[10];
int mp[10][10];
int num;
void init()
{
    memset(mp,0,sizeof(mp));
    mp[1][9]=mp[9][1]=mp[3][7]=mp[7][3]=mp[2][8]=mp[8][2]=mp[4][6]=mp[6][4]=5;
    mp[1][3]=mp[3][1]=2;
    mp[1][7]=mp[7][1]=4;
    mp[7][9]=mp[9][7]=8;
    mp[3][9]=mp[9][3]=6;
}
int ans[400000][10];
void show()
{
    for(int i=0; i<n; i++)
        cout<<may[i]<<' ';
    cout<<endl;
}
bool judge()
{
    int viss[10];
    memset(viss,0,sizeof(viss));
    viss[0]=-1;
    int s1=may[0],s2;
    viss[s1]=1;
    for(int i=1; i<n; i++)
    {
        s2=may[i];
        if(viss[s2]==1)
            return false;
        if(mp[s1][s2]!=0&&viss[mp[s1][s2]]==0)
            return false;
        s1=s2;
        viss[s2]=1;
    }
    for(int i=0; i<n; i++)
    {
        ans[num][i]=may[i];
    }
    num++;
    return true;

}
void dfs(int p)
{
    if(p==n)
    {
        judge();
        return;
    }
    for(int i=0; i<n; i++)
    {
        if(vis[a[i]]==0)
        {
            may[p]=a[i];
            vis[a[i]]=1;
            dfs(p+1);
            vis[a[i]]=0;
        }
    }
}
int main()
{
    init();
    int T;
    cin>>T;
    while(T--)
    {
        num=0;
        cin>>n;
        for(int i=0; i<n; i++)
            scanf("%d",&a[i]);
        sort(a,a+n);
        dfs(0);
        cout<<num<<endl;
        for(int i=0; i<num; i++)
        {
            for(int j=0; j<n-1; j++)
                printf("%d ",ans[i][j]);
            printf("%d\n",ans[i][n-1]);
        }
    }
    return 0;
}

ZOJ 3861 - Valid Pattern Lock的更多相关文章

  1. DFS+模拟 ZOJ 3861 Valid Pattern Lock

    题目传送门 /* 题意:手机划屏解锁,一笔连通所有数字,输出所有可能的路径: DFS:全排列 + ok () 判断函数,去除一些不可能连通的点:) */ #include <cstdio> ...

  2. ACM学习历程—ZOJ 3861 Valid Pattern Lock(dfs)

    Description Pattern lock security is generally used in Android handsets instead of a password. The p ...

  3. ZOJ - 3861 Valid Pattern Lock 【全排列】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3861 思路 先生成全排列,然后判断哪些情况不符合的,剔除就好了 ...

  4. 浙江大学2015年校赛B题 ZOJ 3861 Valid Pattern Lock

    这道题目是队友写的,貌似是用暴力枚举出来. 题意:给出一组数,要求这组数在解锁的界面可能的滑动序列. 思路:按照是否能够直接到达建图,如1可以直接到2,但是1不能直接到3,因为中间必须经过一个2. 要 ...

  5. ZOJ Problem Set - 3861 Valid Pattern Lock(dfs)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3861 这道题当时没做出来,后来经过队友提醒才做出来. 3*3的九宫格,给你 ...

  6. Valid Pattern Lock(dfs + 暴力)

    Valid Pattern Lock Time Limit: 2 Seconds      Memory Limit: 65536 KB Pattern lock security is genera ...

  7. zoj 3861(dfs)

    Valid Pattern Lock Time Limit: 2 Seconds      Memory Limit: 65536 KB Pattern lock security is genera ...

  8. Bypass pattern lock on Sony Xperia Z2 and backup all data

    Yesterday she came to me with a Sony Xperia Z2 D6503. Guess what? She forgot the pattern so she coul ...

  9. Deal with Android phones with pattern lock on

    Yesterday my colleague asked me for help...She has two android phones , one is hTC and the other is ...

随机推荐

  1. linux 命令之 insmod

    man insmod: INSMOD(8) insmod INSMOD(8) NAME insmod - Simple program to insert a module into the Linu ...

  2. Swift游戏实战-跑酷熊猫 06 创建平台类以及平台工厂类

    这节内容我们一起学习下随机长度的踩踏平台的原理是怎么样的. 要点: 平台类 我们的平台类继承于SKNode,这样就能被添加进其它节点进而显示在场景中. 它有一个方法来创建平台,这个方法接收一个包含SK ...

  3. Summary: Final Keyword

    In this tutorial we will learn the usage of final keyword. final keyword can be used along with vari ...

  4. java.面向对象特征

    面向对象特征: 封装,多态,继承 面向对象思想: 封装,继承,多态,接口

  5. spark on mesos 两种运行模式

    spark on mesos 有粗粒度(coarse-grained)和细粒度(fine-grained)两种运行模式,细粒度模式在spark2.0后开始弃用. 细粒度模式 优点 spark默认运行的 ...

  6. PHP与jquery前后台交互的小程序

    1 <!DOCTYPE HTML> <html> <head> <meta charset = "utf-8"> <scrip ...

  7. sql 根据一个表更新 另一个表的例子及可能遇到的问题

    例子: update a set a.name=b.name1 from a,b where a.id=b.id 例子延伸:更新的时候会把字符串 转为科学计数法  怎么办? 答:用 cast 转换一下 ...

  8. Microsoft.Jet.OLEDB.4.0和Microsoft.ACE.OLEDB.12.0的区别

    Microsoft.Jet.OLEDB.4.0和Microsoft.ACE.OLEDB.12.0的区别 时间 2012-12-19 20:30:12  CSDN博客原文  http://blog.cs ...

  9. jquery遍历对象,数组,集合

    1.jquery 遍历对象 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTM ...

  10. Writable、WritableComparable和comparators

    hadoop的序列化格式 hadoop自身的序列化存储格式就是实现了Writable接口的类,他只实现了前面两点,压缩和快速.但是不容易扩展,也不跨语言. 我们先来看下Writable接口,Writa ...