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. Android四大布局及其主要属性

    布局: <LinearLayout></LinearLayout> <RelativeLayout></RelativeLayout> <Fram ...

  2. 两个简单的Loading

    置顶文章:<纯CSS打造银色MacBook Air(完整版)> 上一篇:<JavaScript并非"按值传递"> 作者主页:myvin 博主QQ:85139 ...

  3. Node基础:域名解析DNS(ok)

    写在前面 Nodejs学习手册,基础总结之DNS模块.对从事web开发的同学来说,DNS解析再熟悉不过,在nodejs中也有一个模块可以完成dns解析的工作,使用非常简单.直接进入主题. 域名解析:d ...

  4. Android数据共享

    Android数据共享 在Android应用程序开发的过程中,借助Bundle类对象来传递数据并不是在所有场景下都适用,就那简单的Intent类对象来说,就不能put进Bundle类对象中.当然不能否 ...

  5. js的设计模式

    <Practical Common Lisp>的作者 Peter Seibel 曾说,如果你需要一种模式,那一定是哪里出了问题.他所说的问题是指因为语言的天生缺陷,不得不去寻求和总结一种通 ...

  6. sql 随机函数newid()

    newid()返回的是uniqueidentifier类型的唯一值.newid()每次产生的值都不一样 从表中随机获取前N条记录 select top N *  from table_name ord ...

  7. form表单用ge方式提交时ie显示中文参数乱码

    有网友说 通过给form表单添加accept-charset="gb2312"和 onsubmit="document.charset='gb2312'" 但这 ...

  8. MongoDB 客户端 MongoVue

    直接上图片,图片是按顺序来的 软件下载地址(Windows下的MongoDB客户端MongoVUE 这是最后一个全功能的不收费的版本): http://pan.baidu.com/s/1skYIEq5

  9. Ibatis学习总结5--动态 Mapped Statement

    直接使用 JDBC 一个非常普遍的问题是动态 SQL.使用参数值.参数本身和数据列都 是动态的 SQL,通常非常困难.典型的解决方法是,使用一系列 if-else 条件语句和一连串 讨厌的字符串连接. ...

  10. maven初学(三) SNAPSHOT

    使用场景: 通常一个大型软件是由多个模块组成的,不同的组使用相同应用的不同版本. 在开发阶段,可能经常需要修复bug和优化. 这种情况下就会导致其他组频繁更新代码和pom文件 SANPSHOT SNA ...