Valid Pattern Lock(dfs + 暴力)
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 a1, a2, … 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 + 暴力)的更多相关文章
- DFS+模拟 ZOJ 3861 Valid Pattern Lock
题目传送门 /* 题意:手机划屏解锁,一笔连通所有数字,输出所有可能的路径: DFS:全排列 + ok () 判断函数,去除一些不可能连通的点:) */ #include <cstdio> ...
- ACM学习历程—ZOJ 3861 Valid Pattern Lock(dfs)
Description Pattern lock security is generally used in Android handsets instead of a password. The p ...
- ZOJ 3861 - Valid Pattern Lock
3861 - Valid Pattern Lock Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & ...
- ZOJ Problem Set - 3861 Valid Pattern Lock(dfs)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3861 这道题当时没做出来,后来经过队友提醒才做出来. 3*3的九宫格,给你 ...
- 浙江大学2015年校赛B题 ZOJ 3861 Valid Pattern Lock
这道题目是队友写的,貌似是用暴力枚举出来. 题意:给出一组数,要求这组数在解锁的界面可能的滑动序列. 思路:按照是否能够直接到达建图,如1可以直接到2,但是1不能直接到3,因为中间必须经过一个2. 要 ...
- ZOJ - 3861 Valid Pattern Lock 【全排列】
题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3861 思路 先生成全排列,然后判断哪些情况不符合的,剔除就好了 ...
- ACM: FZU 2107 Hua Rong Dao - DFS - 暴力
FZU 2107 Hua Rong Dao Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I6 ...
- ACM: Gym 100935G Board Game - DFS暴力搜索
Board Game Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Gym 100 ...
- 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 ...
随机推荐
- PCA和LDA降维的比较
PCA 主成分分析方法,LDA 线性判别分析方法,可以认为是有监督的数据降维.下面的代码分别实现了两种降维方式: print(__doc__) import matplotlib.pyplot as ...
- Chrome扩展开发(Gmail附件管理助手)系列之〇——概述
目录: 0.Chrome扩展开发(Gmail附件管理助手)系列之〇——概述 1.Chrome扩展开发之一——Chrome扩展的文件结构 2.Chrome扩展开发之二——Chrome扩展中脚本的运行机制 ...
- 『.Net』微软家的女儿们
在博客园看到了 一篇文章 <.NET4.0框架退休日期逐渐临近> 写下了这篇文章 —— 记录我和 .Net Framework 的 那些日子. 微软 家有几个 女儿. 2008年,我遇到他 ...
- ASP.NET 系列:RBAC权限设计
权限系统的组成通常包括RBAC模型.权限验证.权限管理以及界面访问控制.现有的一些权限系统分析通常存在以下问题: (1)没有权限的设计思路 认为所有系统都可以使用一套基于Table设计的权限系统.事实 ...
- 系统安全扫描工具(appscan)的扫描类型小记
扫描分类 不同场景需要使用不同方式的扫描类型.不能盲目的.暴力的去折腾. 自动扫描 刚开始扫描的时候适合用这种方式.有助于,理解整个网站的结构. 需要注意的是:去伪静态和业务冗余 伪静态 url结构相 ...
- js事件代理
需要注意的blog:http://blog.csdn.net/majian_1987/article/details/8591385 一篇博客看懂 http://blog.csdn.net/maji ...
- 解决HTML5布局,兼容IE问题
当我们使用h5的新标签,header,footer,aside,section,article...时,会遇到低版本IE不兼容问题,如下图: 解决方案:引入如下JS代码,即可 (这里我就直接放源码了, ...
- linux 查看服务器性能常用命令
一.top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器 下面详细介绍它的使用方法.top是一个动态显示过程,即可以通过用户按键来 ...
- 调研Android平台开发环境的发展演变
Android是Google推出的开源手机操作系统,主要以开发应用为主,要进行Android开发首先得搭建好开发平台.最近在搭建Android的开发环境,发现往往一个小问题都能花费你大半天时间,从刚开 ...
- java并发库--锁
synchronized的缺陷: 被synchronized修饰了,当一个线程获取了对应的锁,并执行该代码块时,其他线程便只能一直等待,等待获取锁的线程释放锁,获取线程被阻塞时,没有释放锁会导致等待线 ...