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 ...
随机推荐
- Jenkins入门系列之——00答疑解惑
写在最前的总结:Jenkins其实就是一个工具,这个工具的作用就是调用各种其他的工具来达成你的目的.比如你要获取Subversion上最新的源代码,Jenkins会去调用SVNKIT(插件的核心Jar ...
- Orchard用LiveWriter写博客
本文链接:http://www.cnblogs.com/souther/p/4544241.html Orchard本身提供一个内建的管理面板来写博客,许多人更喜欢采用客户端提交的方式,例如Windo ...
- jQuery UI dialog
初始化参数 对于 dialog 来说,首先需要进行初始化,在调用 dialog 函数的时候,如果没有传递参数,或者传递了一个对象,那么就表示在初始化一个对话框. 没有参数,表示按照默认的设置初始化对话 ...
- 获取、增加、修改、删除sqlserver字段描述
先看添加与删除字段描述 EXEC sys.sp_addextendedproperty @name = N'MS_Description', --添加Type字段说明 @value = N'屏蔽类型对 ...
- javaWeb开发模式
1.发展历程 2.模式分析 JSP+JavaBean模式适合开发业务逻辑不太复杂的web服务程序.这种模式下,JavaBean用于封装业务数据,JSP即负责处理用户请求,又显示数据(JSP编写业务逻辑 ...
- DOM(四)事件流
1.冒泡型事件 浏览器的事件模型分两种:捕获型事件和冒泡型事件.由于ie不支持捕获型事件,所以以下主要以冒泡型事件作为讲解.(dubbed bubbling)冒泡型指事件安装最特定的事件到最不特定的事 ...
- 小记:Quartz 当 Job 执行时间超过触发间隔时间时所发生的情况
一个普通的 Job 实现如下: public class Job1 : IJob { public void Execute(IJobExecutionContext context) { Conso ...
- Daily Scrum – 1/5
Meeting Minutes 开始了新的sprint: 开始准备英语版本的翻译: Progress part 组员 今日工作 Time (h) 明日计划 Time (h) Wei ...
- JQuery学习(2)之Ajax
同步传输 正 多件事情一起做 | 程 不 事情一件一件地做 常: | 序 是: 异步传输 人 事情一件一件地做 | 员 人 多件事情可以一起做 附加(XML) ★XML作用:用来存储数据. ★XML特 ...
- Cas_Server端安装
一.Cas Server版本:3.5.2 下载地址:http://download.csdn.net/detail/xiaohuzi1987/5262980 二.安装步骤: 1.解压cas ...