Description

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

题目就是搜索。一共有8 + 8个方向。

需要注意的是:对于东南西北、东偏南等等的8个方向,如果被标记过,需要再往下搜索一步。

Map初始化需要全部置为-1,表示不可访问。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string> using namespace std; int xx[] = {-, , -, , -, , -, };
int yy[] = {-, -, -, -, , , , };
int Map[][], n, len; struct node
{
int val[];
}p[]; bool cmp(node a, node b)
{
for (int i = ; i < ; ++i)
if (a.val[i] != b.val[i])
return a.val[i] < b.val[i];
} void Dfs(int x, int y, int now)
{
p[len].val[now] = (x-)*+y;
if (now == n - )
{
len++;
p[len] = p[len-];
return;
}
for (int xx = -; xx <= ; ++xx)
{
for (int yy = -; yy <= ; ++yy)
{
switch (Map[x+xx][y+yy])
{
case -:
continue;
case :
Map[x+xx][y+yy] = ;
Dfs(x+xx, y+yy, now+);
Map[x+xx][y+yy] = ;
break;
case :
if (Map[x+xx*][y+yy*] == )
{
Map[x+xx*][y+yy*] = ;
Dfs(x+xx*, y+yy*, now+);
Map[x+xx*][y+yy*] = ;
}
break;
}
}
} for (int i = ; i < ; ++i)
{
if (x+xx[i] > || x+xx[i] < )
continue;
if (y+yy[i] > || y+yy[i] < )
continue;
if (Map[x+xx[i]][y+yy[i]] == )
{
Map[x+xx[i]][y+yy[i]] = ;
Dfs(x+xx[i], y+yy[i], now+);
Map[x+xx[i]][y+yy[i]] = ;
}
}
}
void Work()
{
for (int i = ; i < ; ++i)
for (int j = ; j < ; ++j)
if (Map[i][j] == )
{
Map[i][j] = ;
Dfs(i, j, );
Map[i][j] = ;
}
sort(p, p+len, cmp);
printf("%d\n", len);
for (int i = ; i < len; ++i)
{
for (int j = ; j < n; ++j)
{
if (j)
printf(" ");
printf("%d", p[i].val[j]);
}
printf("\n");
}
} void Input()
{
int v, x, y;
len = ;
memset(Map, -, sizeof(Map));
scanf("%d", &n);
for (int i = ; i < n; ++i)
{
scanf("%d", &v);
x = (v-)/ + ;
y = v%;
if (!y)
y = ;
Map[x][y] = ;
}
/*
for (int i = 0; i < 5; ++i)
{
for (int j = 0;j < 5; ++j)
{
printf("%3d", Map[i][j]);
}
printf("\n");
}
*/
} int main()
{
//freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
int T;
scanf("%d", &T);
for (int times = ; times < T; ++times)
{
Input();
Work();
}
return ;
}

ACM学习历程—ZOJ 3861 Valid Pattern Lock(dfs)的更多相关文章

  1. DFS+模拟 ZOJ 3861 Valid Pattern Lock

    题目传送门 /* 题意:手机划屏解锁,一笔连通所有数字,输出所有可能的路径: DFS:全排列 + ok () 判断函数,去除一些不可能连通的点:) */ #include <cstdio> ...

  2. ZOJ 3861 - Valid Pattern Lock

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

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

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

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

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

  5. Valid Pattern Lock(dfs + 暴力)

    Valid Pattern Lock Time Limit: 2 Seconds      Memory Limit: 65536 KB Pattern lock security is genera ...

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

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

  7. ACM学习历程——ZOJ 3829 Known Notation (2014牡丹江区域赛K题)(策略,栈)

    Description Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathema ...

  8. ACM学习历程—ZOJ 3868 GCD Expectation(莫比乌斯 || 容斥原理)

    Description Edward has a set of n integers {a1, a2,...,an}. He randomly picks a nonempty subset {x1, ...

  9. ACM学习历程—ZOJ 3777 Problem Arrangement(递推 && 状压)

    Description The 11th Zhejiang Provincial Collegiate Programming Contest is coming! As a problem sett ...

随机推荐

  1. iPhone换电池是原装电池好还是换第三方大容量电池好?

    转:https://www.xianjichina.com/news/details_60791.html 最近这段时间苹果降速门事件持续发酵,闹得满城风雨.尽管苹果公司两次致歉,很多果粉都去更换电池 ...

  2. Redis(六):java里常用的redis客户端(Jedis和Redisson)

    Redis的各种语言客户端列表,请参见Redis Client.其中Java客户端在github上start最高的是Jedis和Redisson.Jedis提供了完整Redis命令,而Redisson ...

  3. MP4文件格式的解析,以及MP4文件的分割算法

    http://www.cnblogs.com/haibindev/archive/2011/10/17/2214518.html http://blog.csdn.net/pirateleo/arti ...

  4. Boost.Asio c++ 网络编程翻译(11)

    *_at方法 这些方法在一个流上面做随机存取操作.你来指定read和write操作从什么地方開始(offset): async_read_at(stream, offset, buffer [, co ...

  5. 嵌入式驱动开发之---Linux ALSA音频驱动(一)

    本文的部分内容参考来自DroidPhone的博客(http://blog.csdn.net/droidphone/article/details/6271122),关于ALSA写得很不错的文章,只是少 ...

  6. 3_Jsp标签_简单标签_防盗链和转义标签的实现

    一概念 1防盗链 在HTTP协议中,有一个表头字段叫referer,采用URL的格式来表示从哪儿链接到当前的网页或文件,通过referer,网站可以检测目标网页访问的来源网页.有了referer跟踪来 ...

  7. H2 database 操作操作内存表

    本例开发工具为 NetBeans,使用b2前提安装jdk. 第一步:在官网下载驱动包 :http://www.h2database.com ,本例版本为: h2-1.4.192.jar 第二步:安装开 ...

  8. 两个DataGridEHToExcel

    procedure TForm1.N1Click(Sender: TObject); var    GridtoExcel: TDBGridEhToExcel; begin    try    Gri ...

  9. 九度OJ 1154:Jungle Roads(丛林路径) (最小生成树)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:832 解决:555 题目描述: The Head Elder of the tropical island of Lagrishan has ...

  10. opencv常用类总结

    1 Rect_ (const Point_< _Tp > &pt1, const Point_< _Tp > &pt2),Rect的这种两个点的构造函数的两个点 ...