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. vim 处理换行符

    1. 设置文件格式 :set fileformats=unix,dos 2. 查询当前文件格式 :set fileformat? 3. 转换文件格式 :set fileformat=dos 4. 设置 ...

  2. win10+vs2017+asp.net MVC5+EF6+mysql 闪退问题,解决方法

    1.安装 mysql-for-visualstudio-2.0.5.msi 2.安装 mysql-connector-net-6.10.7.msi 3.在VS2017 右键选中项目,管理NuGet程序 ...

  3. Away3D引擎学习笔记(一)资源加载解析块

    前文:Away3D断断续续用了一段时间了,三维相关的很多算法,计算转换还是有点绕,整理些自己觉得还有点意思东西,希望大家有用. 三维开始,Away3D构架你场景那几行代码各处都有,这里就不copy了, ...

  4. Mysql 5.7.18 利用 MySQL proxies_priv(模拟角色)实现类似用户组管理

    利用 MySQL proxies_priv(模拟角色)实现类似用户组管理 角色(Role)可以用来批量管理用户,同一个角色下的用户,拥有相同的权限. MySQL5.7.X以后可以模拟角色(Role)的 ...

  5. A20地址线问题

    [0]README text description from Zhaojiong's perfect analysis of Linux kernel . [1]A20地址线问题(干货来了) 198 ...

  6. <转载> pycharm快捷键及一些常用设置

    1.编辑(Editing ) Ctrl + Space 基本的代码完成(类.方法.属性)Ctrl + Alt + Space 快速导入任意类Ctrl + Shift + Enter 语句完成Ctrl ...

  7. 使用jQuery Ajax功能的时候需要注意的一个问题

    每次jquery的Ajax请求都会创建一个xmlHttprequest对象,理论上讲,长连接(页面需要和服务器保持长连接,而且在连接超时后需要重新请求连接)的请求是一个无限递归,请求数量是非常大的,但 ...

  8. 九度OJ 1020:最小长方形 (基础题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6019 解决:2849 题目描述:     给定一系列2维平面点的坐标(x, y),其中x和y均为整数,要求用一个最小的长方形框将所有点框在 ...

  9. AWS:1.相关概念、创建云主机的过程

    概念 EC2是弹性的云计算 云主机 也即虚拟机,由分配的CPU.内存.网络和磁盘等资源组成 好处:维护成本低(主机替换).环境升级成本低 AMI:映像 创建云主机的蓝图,指定初始状态1 预装什么操作系 ...

  10. 我的Android进阶之旅------>解决如下错误failed to copy 'Settings2.apk' to '/system/app//Settings2.apk': Read-only

    push apk的时候报错 ouyangpeng@oyp-ubuntu:~/apk升级$ adb push Settings2.apk /system/app/ failed to copy 'Set ...