ACM学习历程—ZOJ 3861 Valid Pattern Lock(dfs)
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)的更多相关文章
- DFS+模拟 ZOJ 3861 Valid Pattern Lock
题目传送门 /* 题意:手机划屏解锁,一笔连通所有数字,输出所有可能的路径: DFS:全排列 + ok () 判断函数,去除一些不可能连通的点:) */ #include <cstdio> ...
- ZOJ 3861 - Valid Pattern Lock
3861 - Valid Pattern Lock Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & ...
- ZOJ - 3861 Valid Pattern Lock 【全排列】
题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3861 思路 先生成全排列,然后判断哪些情况不符合的,剔除就好了 ...
- 浙江大学2015年校赛B题 ZOJ 3861 Valid Pattern Lock
这道题目是队友写的,貌似是用暴力枚举出来. 题意:给出一组数,要求这组数在解锁的界面可能的滑动序列. 思路:按照是否能够直接到达建图,如1可以直接到2,但是1不能直接到3,因为中间必须经过一个2. 要 ...
- Valid Pattern Lock(dfs + 暴力)
Valid Pattern Lock Time Limit: 2 Seconds Memory Limit: 65536 KB Pattern lock security is genera ...
- ZOJ Problem Set - 3861 Valid Pattern Lock(dfs)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3861 这道题当时没做出来,后来经过队友提醒才做出来. 3*3的九宫格,给你 ...
- 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 ...
- ACM学习历程—ZOJ 3868 GCD Expectation(莫比乌斯 || 容斥原理)
Description Edward has a set of n integers {a1, a2,...,an}. He randomly picks a nonempty subset {x1, ...
- ACM学习历程—ZOJ 3777 Problem Arrangement(递推 && 状压)
Description The 11th Zhejiang Provincial Collegiate Programming Contest is coming! As a problem sett ...
随机推荐
- C 语言学习 3
[程序3] 题目:一个整数,它加上100后是一个全然平方数.再加上168又是一个全然平方数.请问该数是多少? 1.程序分析:在10万以内推断.先将该数加上100后再开方,再将该数加上268后再开方,假 ...
- Java性能小技巧
局部决定总体. 一个应用的总体性能取决于每一个组件的性能. 以下是一些帮助你提高应用性能的Java编程技巧: 编程技巧 原因及策略 避免反复创建对象 为什么: 更少的对象会须要更少的垃圾回收 使用的空 ...
- 无刷新URL 更新
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- request 获取请求头
/********************************************************servlet页面********************************** ...
- 记一次Oracle数据故障排除过程
前天在Oracle生产环境中,自己的存储过程运行时间超过1小时,怀疑是其他job运行时间过长推迟了自己job运行时间,遂重新跑job,发现同测试环境的确不同,运行了25分钟. 之后准备在测试环境中制造 ...
- Silverlight 5 Grid组的MouseLeave响应
用Silverlight 5作个用户控件,即是用Grid画几个格子.分别显示几张透明图片.效果是显示中间那张,点击显示的图片后将其他几张图片一起显示出来,鼠标立马这个用户控件范围后自己主动隐藏点击后显 ...
- golang手动管理内存
作者:John Graham-Cumming. 原文点击此处.翻译:Lubia Yang(已失效) 前些天我介绍了我们对Lua的使用,implement our new Web Applicati ...
- java8笔记: sorted()之正序倒序
java8笔记: sorted()之正序倒序 这篇文章将会讲解Java 8 Stream sorted()示例 下面代码以自然序排序一个list List<Person> listTem ...
- Android setTag()与getTag(),与set多个setTag()
首先我们要知道setTag方法是干什么的,SDK解释为 Tags Unlike IDs, tags are not used to identify views. Tags are essential ...
- 解决UICollectionView的Cell复用引起的布局混乱问题
解决UICollectionView的Cell复用引起的布局混乱问题 问题复现.gif 查了一下度娘发现没有好的解决办法,于是发动自己的聪明才智,终于找到如下解决办法(充分证明了自己动手丰衣足食啊