题目链接

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 【全排列】的更多相关文章

  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. ACM学习历程—ZOJ 3861 Valid Pattern Lock(dfs)

    Description Pattern lock security is generally used in Android handsets instead of a password. The p ...

  4. 浙江大学2015年校赛B题 ZOJ 3861 Valid Pattern Lock

    这道题目是队友写的,貌似是用暴力枚举出来. 题意:给出一组数,要求这组数在解锁的界面可能的滑动序列. 思路:按照是否能够直接到达建图,如1可以直接到2,但是1不能直接到3,因为中间必须经过一个2. 要 ...

  5. ZOJ Problem Set - 3861 Valid Pattern Lock(dfs)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3861 这道题当时没做出来,后来经过队友提醒才做出来. 3*3的九宫格,给你 ...

  6. Valid Pattern Lock(dfs + 暴力)

    Valid Pattern Lock Time Limit: 2 Seconds      Memory Limit: 65536 KB Pattern lock security is genera ...

  7. zoj 3861(dfs)

    Valid Pattern Lock Time Limit: 2 Seconds      Memory Limit: 65536 KB Pattern lock security is genera ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. leetCode 58.Length of Last Word (最后单词的长度) 解题思路和方法

    Length of Last Word  Given a string s consists of upper/lower-case alphabets and empty space charact ...

  2. Elite Container DELPHI下的一个轻量级IoC对象容器

    一.简介: Elite Container是DELPHI下的一个轻量级IoC对象容器(IoC:Inverse of Control,反转控制).它是参考了Java中的Spring框架(主要是配置文件的 ...

  3. [Android实例] Scroll原理-附ScrollView源码分析 (转载)

    想象一下你拿着放大镜贴很近的看一副巨大的清明上河图, 那放大镜里可以看到的内容是很有限的, 而随着放大镜的上下左右移动,就可以看到不同的内容了 android中手机屏幕就相当于这个放大镜, 而看到的内 ...

  4. windows 控制台cmd乱码的解决办法

    windows 控制台cmd乱码的解决办法 我本机的系统环境: OS Name: Microsoft Windows 10 企业版 OS Version: 10.0.14393 N/A Build 1 ...

  5. 一个JavaScript Function Outliner插件 第三个版本 让你的JavaScript代码也支持折叠

    下面我只以英文的vs2008版本作为实例,演示一下打开vs2008 然后一次点击:Tools->Options (工具->选项)会弹出选项设置框在左边的树目录里展开Environment- ...

  6. java程序如何优化--技巧总结

    http://www.douban.com/group/topic/17850695/

  7. [javase学习笔记]-8.2 成员变量与静态变量的差别

    这一节我们看一看成员变量与静态变量的差别所在. 什么是静态变量呢?我们上节用statickeyword时就提到了静态变量.也就是说用statickeyword修饰的变量就是静态变量. 我们在6.4节学 ...

  8. DDR电源硬件设计要点

    一.DDR电源简介 1. 电源 DDR的电源可以分为三类: a.主电源VDD和VDDQ,主电源的要求是VDDQ=VDD,VDDQ是给IO buffer供电的电源,VDD是给但是一般的使用中都是把VDD ...

  9. HTML5 2D平台游戏开发#2跳跃与二段跳

    在上一篇<Canvas制作时间与行为可控的sprite动画>中已经实现了角色的左右移动,本篇继续实现角色的一系列动作之一:跳跃.先来看看最终效果: 要实现跳跃,必须模拟垂直方向的速度和重力 ...

  10. rtems 4.11 IRQ (arm,beagle)

    arm IRQ入口在 cpukit/score/arm/arm_exec_interrupt.S 中,其中BSP最关心就是 bl bsp_interrupt_dispatch 这句,看看beagle ...