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. Java 加载器

    类的加载是由类加载器完成的,类加载器包括: 根加载器( BootStrap ).扩展加载器( Extension ).系统加载器( System )和用户自定义类加载器( java.lang.Clas ...

  2. Angular+Angular-Ui实现分页(代码更加简单,更加容易懂哦)

    前面写了个分页,但是个人认为只能玩玩,实际业务上的话代码太繁杂(笔者想走代码.性能精简化之路[不知道哪天才能成为高手~·YY一下无伤大雅]),逻辑上有点混乱.那么今天我们来看看另外一种只实现分页没有查 ...

  3. win10系统架构调用

    操作系统模型 操作系统有两种模式: 用户模式 内核模式 当用户模式调用系统服务时,CPU执行一个特殊的指令以切换到内核模式(Ring0),当系统服务调用完成时,操作系统切换回用户模式(Ring3).  ...

  4. 获取网站的BaseURL

    //get base URL            var _urlstr = window.location.href;            if (_urlstr.indexOf("? ...

  5. python 基础 1.2--pycharm 的安装及使用

    一. windows 先安装pycharm. PyCharm是一种Python IDE,带有一整套可以帮助用户在使用Python语言开发时提高其效率的工具,其提供了一个带编码补全,代码片段,支持代码折 ...

  6. webstorm vscode 常用设置

    webstorm常用的设置及操作图解 VS Code 新建vue文件初始化模板 VSCode新建vue文件自定义模板

  7. <转> Struct 和 Union区别 以及 对内存对齐方式的说明

    转载地址:http://blog.csdn.net/firefly_2002/article/details/7954458 一.Struct 和 Union有下列区别: 1.在存储多个成员信息时,编 ...

  8. WebApi基础

    1:当Controller中有相同参数的方法时,请求调用会报错 [HttpGet] public IEnumerable<string> Resturn() { return new st ...

  9. haproxy + keepalived 实现web 双主模型的高可用负载均衡

    参考文章 http://xz159065974.blog.51cto.com/8618592/1405812 http://blog.chinaunix.net/uid-25266990-id-398 ...

  10. HNOI2016

    本蒟蒻表示终于$AC$了$HNOI2016$的六道毒瘤题... 高兴! 附上各个题的题解: $DAY1$: $T1$: BZOJ4537: [Hnoi2016]最小公倍数 $T2$: BZOJ4538 ...