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. 我的权限系统设计实现MVC4 + WebAPI + EasyUI + Knockout(三)图形化机构树

    一.前言 组织机构是国内管理系统中很重要的一个概念,以前我们基本都是采用数据列表的形式展现,最多只是采用树形列表展现.虽然够用,但是如果能做成图形化当然是最好不过了.这里我不用任何图形控件,就用最原始 ...

  2. 【WEB API项目实战干货系列】- API登录与身份验证(三)

    上一篇: [WEB API项目实战干货系列]- 接口文档与在线测试(二) 这篇我们主要来介绍我们如何在API项目中完成API的登录及身份认证. 所以这篇会分为两部分, 登录API, API身份验证. ...

  3. IT男的”幸福”生活"续3

    我和MM一进饭店,服务员走过,面带笑容,说:“欢迎,欢迎,两位里面坐.” ...... 谢谢大家的捧场,IT的”幸福”生活.是我的回忆录来着.真实可靠,在写法上有点小说化.可能是我一直看小说的原因吧, ...

  4. C# txt格式记录时间,时间对比,决定是否更新代码记录Demo

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  5. “耐撕”团队 2016.04.05 站立会议

    1. 时间: 20:10--20:25  共计15分钟. 2. 成员: Z 郑蕊 * 组长 (博客:http://www.cnblogs.com/zhengrui0452/), P 濮成林(博客:ht ...

  6. C头文件之<stdio.h>

    (stdio.h) 该头文件主要是执行输入输出操作.文件中重要的概念是“流”(streams).“流”在函数库中用FILE表示,用指针类型FILE *来操作.有三个标准流:stdin, stdout, ...

  7. du 命令

    Linux du命令也是查看使用空间的,但是与df命令不同的是Linux du命令是对文件和目录磁盘使用的空间的查看,还是和df命令有一些区别的. 1.命令格式: du [选项][文件] 2.命令功能 ...

  8. Java编程思想学习(八) 内部类

    可以将一个类的定义放在另一个类的定义内部,这就是内部类. 内部类的定义是简单的,但是它的语法确实很是复杂,让人不是很好理解.下面就内部类做一个小结. 一.内部类的分类 总的来讲内部类分为普通内部类,匿 ...

  9. SQLServer 开启远程访问,也可逆向思维进行关闭

    为了可以通过TCP/IP协议远程访问SQLServer数据库,需要做以下几点: 在SQLServer所运行的服务器上,我们必须找到SQLServer所侦听的端口然后添加到WIndows防火墙的[允许入 ...

  10. 如何在 Arch Linux 的终端里设定 WiFi 网络

    如果你使用的是其他 Linux 发行版 而不是 Arch CLI,那么可能会不习惯在终端里设置 WiFi.尽管整个过程有点简单,不过我还是要讲一下.在这篇文章里,我将带领新手们通过一步步的设置向导,把 ...