ZOJ - 3861 Valid Pattern Lock 【全排列】
题目链接
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3861
思路
先生成全排列,然后判断哪些情况不符合的,剔除就好了
代码中 init() 部分 就是 先指明 哪两个数字之间 是必须有另外一个数字的
AC代码
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>
#define CLR(a) memset(a, 0, sizeof(a))
#define pb push_back
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss;
const double PI = acos(-1);
const double E = exp(1);
const double eps = 1e-30;
const int INF = 0x3f3f3f3f;
const int maxn = 2e5 + 5;
const int MOD = 1e9 + 7;
int n;
int c[10][10];
int vis[10];
bool judge(vector <int> ans)
{
for (int i = 0; i < n - 1; i++)
{
if (c[ans[i]][ans[i + 1]] && vis[c[ans[i]][ans[i + 1]]] == 0)
return false;
vis[ans[i]] = 1;
}
return true;
}
void init()
{
CLR(c);
c[1][3] = 2, c[3][1] = 2;
c[1][7] = 4, c[7][1] = 4;
c[1][9] = 5, c[9][1] = 5;
c[2][8] = 5, c[8][2] = 5;
c[3][9] = 6, c[9][3] = 6;
c[3][7] = 5, c[7][3] = 5;
c[4][6] = 5, c[6][4] = 5;
c[7][9] = 8, c[9][7] = 8;
}
int main()
{
init();
int t;
cin >> t;
while (t--)
{
scanf("%d", &n);
vector <int> pre;
int num;
for (int i = 0; i < n; i++)
{
scanf("%d", &num);
pre.pb(num);
}
sort(pre.begin(), pre.end());
vector < vector <int> > ans;
do
{
CLR(vis);
if (judge(pre))
ans.pb(pre);
} while (next_permutation(pre.begin(), pre.end()));
int len = ans.size();
printf("%d\n", len);
for (int i = 0; i < len; i++)
{
for (int j = 0; j < n; j++)
{
if (j)
printf(" ");
printf("%d", ans[i][j]);
}
printf("\n");
}
}
}
ZOJ - 3861 Valid Pattern Lock 【全排列】的更多相关文章
- 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 & ...
- ACM学习历程—ZOJ 3861 Valid Pattern Lock(dfs)
Description Pattern lock security is generally used in Android handsets instead of a password. The p ...
- 浙江大学2015年校赛B题 ZOJ 3861 Valid Pattern Lock
这道题目是队友写的,貌似是用暴力枚举出来. 题意:给出一组数,要求这组数在解锁的界面可能的滑动序列. 思路:按照是否能够直接到达建图,如1可以直接到2,但是1不能直接到3,因为中间必须经过一个2. 要 ...
- ZOJ Problem Set - 3861 Valid Pattern Lock(dfs)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3861 这道题当时没做出来,后来经过队友提醒才做出来. 3*3的九宫格,给你 ...
- Valid Pattern Lock(dfs + 暴力)
Valid Pattern Lock Time Limit: 2 Seconds Memory Limit: 65536 KB Pattern lock security is genera ...
- zoj 3861(dfs)
Valid Pattern Lock Time Limit: 2 Seconds Memory Limit: 65536 KB Pattern lock security is genera ...
- Bypass pattern lock on Sony Xperia Z2 and backup all data
Yesterday she came to me with a Sony Xperia Z2 D6503. Guess what? She forgot the pattern so she coul ...
- Deal with Android phones with pattern lock on
Yesterday my colleague asked me for help...She has two android phones , one is hTC and the other is ...
随机推荐
- mac复制文件命令
test1下有test01 test02两个文件 ,复制到test2下 则cp -r test1/ test2 权限不够,,则加sudo test2要事先存在, 如果复制test01到当前目录 cp ...
- MySQL主从同步异常问题解决Client requested master to start replication from position > file size
MySQL主从同步异常问题解决Client requested master to start replication from position > file size 一.问题描述 MySQ ...
- [Golang] 从零開始写Socket Server(2): 自己定义通讯协议
在上一章我们做出来一个最基础的demo后,已经能够初步实现Server和Client之间的信息交流了~ 这一章我会介绍一下怎么在Server和Client之间实现一个简单的通讯协议.从而增强整个信息交 ...
- springboot + mybatis配置多数据源示例
转:http://www.jb51.net/article/107223.htm 在实际开发中,我们一个项目可能会用到多个数据库,通常一个数据库对应一个数据源. 代码结构: 简要原理: 1)Datab ...
- mysql left join中on后加条件判断和where中加条件的区别
left join中关于where和on条件的几个知识点: .多表left join是会生成一张临时表,并返回给用户 .where条件是针对最后生成的这张临时表进行过滤,过滤掉不符合where条件的记 ...
- 请求php文件的整个流程
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...
- TP5结合聚合数据API查询天气
php根据城市查询天气情况看到有人分享java的查询全国天气情况的代码,于是我想分享一个php版本的查询天气接口.免费查询天气的接口有很多,比如百度的apistore的天气api接口,我本来想采用这个 ...
- scramble-string——两个字符串经过树化并旋转后是否一致、递归、动态规划
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- xshell的快捷键(很有用)
删除 ctrl + d 删除光标所在位置上的字符相当于VIM里x或者dl ctrl + h 删除光标所在位置前的字符相当于VIM里hx或者dh ctrl + k 删除光标 ...
- UML--组件图,部署图
组件图用于实现代码之间的物理结构,详细来说,就是实现代码交互.通过接口,将不同的软件,程序连接在一起. [理解] 1.组件的定义相当广泛,包含:源码,子系统,动态链接库,Activex控件. 2.组件 ...