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.

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<stdio.h>
#include<string.h>
#include<algorithm>
int T ;
int n , a[] ;
int b[] ;
bool vis[] ;
bool blog[] ;
bool flag = ;
int cnt = ;
int ans[ + ][] ; bool judge (int sx , int ex)
{
if (sx == && ex == || sx == && ex == ) {
if (blog[]) return true ;
return false ;
}
else if (sx == && ex == || sx == && ex == ) {
if (blog[]) return true ;
return false ;
}
else if (sx == && ex == || sx == && ex == ) {
if (blog[]) return true ;
return false ;
}
else if (sx == && ex == || sx == && ex == ) {
if (blog[]) return true ;
return false ;
}
else if (sx == && ex == || sx == && ex == ) {
if (blog[]) return true ;
return false ;
}
else if (sx == && ex == || sx == && ex == ) {
if (blog[]) return true ;
return false ;
}
else if (sx == && ex == || sx == && ex == ) {
if (blog[]) return true ;
return false ;
}
else if (sx == && ex == || sx == && ex == ) {
if (blog[]) return true ;
return false ;
}
return true ;
} void dfs (int deep)
{
if (deep == n + ) {
memset (blog , , sizeof(blog)) ;
flag = ;
blog[b[]] = ;
for (int i = ; i <= n && flag; i++) {
flag = judge (b[i - ] , b[i]) ;
blog[b[i]] = ;
}
if (flag) {
for (int i = ; i <= n ; i++) {
ans[cnt][i] = b[i] ;
}
cnt ++ ;
}
/* for (int i = 1 ; i <= n ; i++) {
printf ("%d " , a[i]);
}
puts ("") ;*/
return ;
}
for (int i = ; i <= n ; i++) {
if (vis[a[i]] == ) {
vis[a[i]] = ;
b[deep] = a[i] ;
dfs (deep + ) ;
vis[a[i]] = ;
}
}
} int main ()
{
//freopen ("a.txt" , "r" , stdin ) ;
scanf ("%d" , &T) ;
while (T --) {
scanf ("%d" , &n) ;
for (int i = ; i <= n ; i++) {
scanf ("%d" , &a[i]);
}
cnt = ;
std::sort (a + , a + n + ) ;
for (int i = ; i <= n ; i++) {
memset (vis , , sizeof(vis)) ;
vis[a[i]] = ;
b[] = a[i] ;
dfs () ;
}
printf ("%d\n" , cnt) ;
for (int i = ; i < cnt ; i++) {
for (int j = ; j <= n ; j++) {
printf ("%d%c" , ans[i][j] , j == n ? '\n' : ' ') ;
}
}
}
return ;
}

Valid Pattern Lock(dfs + 暴力)的更多相关文章

  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

    3861 - Valid Pattern Lock Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & ...

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

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

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

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

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

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

  7. ACM: FZU 2107 Hua Rong Dao - DFS - 暴力

    FZU 2107 Hua Rong Dao Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

  8. ACM: Gym 100935G Board Game - DFS暴力搜索

    Board Game Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u  Gym 100 ...

  9. 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 ...

随机推荐

  1. 20145312 GDB调试汇编堆栈过程分析

    20145312 GDB调试汇编堆栈过程分析 参考资料 卢肖明同学的博客:<GDB调试汇编堆栈过程分析>: http://www.cnblogs.com/lxm20145215----/p ...

  2. 【APUE】Chapter17 Advanced IPC & sign extension & 结构体内存对齐

    17.1 Introduction 这一章主要讲了UNIX Domain Sockets这样的进程间通讯方式,并列举了具体的几个例子. 17.2 UNIX Domain Sockets 这是一种特殊s ...

  3. 编写高质量代码改善C#程序的157个建议[动态数组、循环遍历、对象集合初始化]

    前言   软件开发过程中,不可避免会用到集合,C#中的集合表现为数组和若干集合类.不管是数组还是集合类,它们都有各自的优缺点.如何使用好集合是我们在开发过程中必须掌握的技巧.不要小看这些技巧,一旦在开 ...

  4. 第一课:js命名空间的介绍,js对象的扩展以及js数组化

    1.命名空间: js里面的命名空间就是使用对象的属性来扩展的.比如,用户定义一个A对象,A对象下面有B属性和C属性,同时B属性和C属性又是对象.因此A={B:{},C:{}},这时用户就可以在B对象和 ...

  5. 'UserInfoBLL' node cannot be resolved for the specified context [MVC展示数据.Controllers.LoginController]问题解决

    我们在配置Spring.Net时,往往会提示找不到,然而看了看都对着呢?那么问题出在了哪? 问题呈现: 进行如下修改,将名字随便取个,不为BLL方法名字即可,我这里添加了一个1,注意这里改了,控制器里 ...

  6. MongoDB 2.6设置访问权限、设置用户

    MongoDB已经使用很长一段时间了,基于MongoDB的数据存储也一直没有使用到权限访问(MongoDB默认设置为无权限访问限制),今天特地花了一点时间研究了一下,研究成果如下: 注:研究成果基于W ...

  7. Qt模型/视图框架----简单的例子

    #include<qapplication.h> #include<qfilesystemmodel.h> #include<qtreeview.h> #inclu ...

  8. poj1470 LCA Tarjan

    比较直接的题目,入门一下. #include<map> #include<queue> #include<stack> #include<cmath> ...

  9. 【kAriOJ】离散数学春季学期编程测试 1

    A.凯撒密码 题意: 给你k1,k2,和一串明文,一串密文. 明文用k1加密,密文用k2解密. 对于明文要把字母转换成大写字母,非字母全部删除. 额:要考虑到取模可能会变成负数,所以要加一下26再取模 ...

  10. codevs2606 约数和问题

    题目描述 Description Smart最近沉迷于对约数的研究中. 对于一个数X,函数f(X)表示X所有约数的和.例如:f(6)=1+2+3+6=12.对于一个X,Smart可以很快的算出f(X) ...