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 ...
随机推荐
- OpenCV实现图像颜色特征提取
https://github.com/ictlyh/ImageFeature 链接:http://pan.baidu.com/s/1mhUoPxI 密码:3cnn
- 智能手机的耗电特征及APP耗电量测试的两种方法
文章陈述了手机发展趋势及耗电特性,集中讨论了时下最为关心的智能手机耗电问题,并介绍了测量手机软件耗电量的两种方法.此外还解释了为何运营商此前会提出收取微信的费用,心跳机制是什么. 美国著名手机公司Pa ...
- Mac中遇到的Eclipse连接不上mySql的问题
1.首先我们在eclipse中连接数据库的过程中,遇到的问题就是如上图.开始百度Communications link failure 这几个关键字.得到的结果基本上就是基本配置参数wait_time ...
- servletRequest 常用操作
package request; import java.io.IOException;import javax.servlet.ServletException;import javax.servl ...
- IPv4地址(一)概述
IPv4地址的长度是多少? IPv4地址是如何表示的? IPv4地址的构成以及每一部分所起到的作用和占的位数特点? IPv4地址长度为32位. IPv4地址分为两部分:网络号和主机号 网络号部分惟一地 ...
- 你的JVM还好吗?GC初步诊断
你的JVM还好吗?GC初步诊断 阿飞的博客 JVM的GC机制绝对是很多程序员的福音,它让Java程序员省去了自己回收垃圾的烦恼.从而可以把大部分时间专注业务身上,大大提高了业务开发速度,让产品 ...
- PHP合并数组+与array_merge的区别
http://www.phpernote.com/php-string/351.html PHP中合并两个数组可以使用+或者array_merge,但这两个还是有区别的 主要区别是当两个或者多个数 ...
- Docker入门系列1:简介
可以实现快速部署. 比如一台 16 核 32G 内存的虚拟机上,需要跑 500+ 个用户的应用(每个应用的功能可以认为是一个网站 + 一系列的 RESTful API),有两个事情很重要: 资源隔离: ...
- spring boot json 首字母大小写问题解决方案
spring boot默认使用的json解析框架是jackson,对于.net转java的项目来说太坑了,首字母大写的属性会自动转为小写,然后前端就悲剧了,十几个属性的ViewModel增加几个Js ...
- EF学习和使用(三)Code First
Code First模式我们称之为"代码优先"模式.从某种角度来看.其实"Code First"和"Model First"区别并非太明显. ...