zoukankan      html  css  js  c++  java
  • ZOJ 3861 Valid Pattern Lock

    Valid Pattern Lock

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    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.

    valid_pattern_lock

    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 a1a2, … 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
    

    Author: LIN, Xi
    Source: The 15th Zhejiang University Programming Contest

     

    解题:直接暴力,够暴力噢

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 int pass[11][11] = {0},d[500000][11];
     4 bool vis[11];
     5 int n,tot,path[11],a[11];
     6 vector<int>ans;
     7 void dfs(int k) {
     8     if(k >= n) {
     9         for(int i = 0; i < n; ++i)
    10             d[tot][i] = path[i];
    11         tot++;
    12         return;
    13     }
    14     for(int i = 0; i < n; ++i)
    15         if(!vis[a[i]]) {
    16             vis[a[i]] = true;
    17             path[k] = a[i];
    18             dfs(k+1);
    19             vis[a[i]] = false;
    20         }
    21 }
    22 void init() {
    23     pass[1][2] = pass[2][1] = 10;
    24     pass[1][3] = pass[3][1] = 2;
    25     pass[2][3] = pass[3][2] = 10;
    26     pass[4][5] = pass[5][4] = 10;
    27     pass[4][6] = pass[6][4] = 5;
    28     pass[5][6] = pass[6][5] = 10;
    29     pass[7][8] = pass[8][7] = 10;
    30     pass[7][9] = pass[9][7] = 8;
    31     pass[8][9] = pass[9][8] = 10;
    32     pass[1][4] = pass[4][1] = 10;
    33     pass[1][7] = pass[7][1] = 4;
    34     pass[4][7] = pass[7][4] = 10;
    35     pass[2][5] = pass[5][2] = 10;
    36     pass[2][8] = pass[8][2] = 5;
    37     pass[5][8] = pass[8][5] = 10;
    38     pass[3][6] = pass[6][3] = 10;
    39     pass[3][9] = pass[9][3] = 6;
    40     pass[6][9] = pass[9][6] = 10;
    41     pass[1][5] = pass[5][1] = 10;
    42     pass[1][9] = pass[9][1] = 5;
    43     pass[5][9] = pass[9][5] = 10;
    44     pass[2][6] = pass[6][2] = 10;
    45     pass[4][8] = pass[8][4] = 10;
    46     pass[5][9] = pass[5][9] = 10;
    47     pass[2][4] = pass[4][2] = 10;
    48     pass[3][5] = pass[5][3] = 10;
    49     pass[5][7] = pass[7][5] = 10;
    50     pass[3][7] = pass[7][3] = 5;
    51     pass[5][7] = pass[7][5] = 10;
    52     pass[6][8] = pass[8][6] = 10;
    53     pass[1][6] = pass[6][1] = 10;
    54     pass[3][4] = pass[4][3] = 10;
    55     pass[4][9] = pass[9][4] = 10;
    56     pass[6][7] = pass[7][6] = 10;
    57     pass[1][8] = pass[8][1] = 10;
    58     pass[3][8] = pass[8][3] = 10;
    59     pass[2][7] = pass[7][2] = 10;
    60     pass[2][9] = pass[9][2] = 10;
    61 }
    62 void test(int o) {
    63     bool done[11] = {false};
    64     done[d[o][0]] = done[10] = true;
    65     int i;
    66     for(i = 1; i < n; ++i)
    67         if(!done[pass[d[o][i-1]][d[o][i]]]) break;
    68         else done[d[o][i]] = true;
    69     if(i == n) ans.push_back(o);
    70 }
    71 int main() {
    72     init();
    73     //freopen("ans.txt","w",stdout);
    74     int T;
    75     scanf("%d",&T);
    76     while(T--) {
    77         scanf("%d",&n);
    78         ans.clear();
    79         for(int i = tot = 0; i < n; ++i)
    80             scanf("%d",a+i);
    81         sort(a,a+n);
    82         dfs(0);
    83         for(int i = 0; i < tot; ++i) test(i);
    84         printf("%d
    ",ans.size());
    85         for(int i = 0; i < ans.size(); ++i) {
    86             for(int j = 0; j < n; ++j) {
    87                 printf("%d%c",d[ans[i]][j],j + 1 == n?'
    ':' ');
    88             }
    89         }
    90     }
    91     return 0;
    92 }
    View Code

    上面的代码有些啰嗦啊

    这个更简练些

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 int pass[11][11] = {0},d[500000][11];
     4 bool vis[11];
     5 int n,tot,path[11],a[11];
     6 void dfs(int k,int pre) {
     7     if(k >= n) {
     8         for(int i = 0; i < n; ++i)
     9             d[tot][i] = path[i];
    10         tot++;
    11         return;
    12     }
    13     for(int i = 0; i < n; ++i)
    14         if(!vis[a[i]]) {
    15             if(vis[pass[pre][a[i]]]) {
    16                 vis[a[i]] = true;
    17                 path[k] = a[i];
    18                 dfs(k+1,a[i]);
    19                 vis[a[i]] = false;
    20             }
    21         }
    22 }
    23 void init() {
    24     pass[1][2] = pass[2][1] = 10;
    25     pass[1][3] = pass[3][1] = 2;
    26     pass[2][3] = pass[3][2] = 10;
    27     pass[4][5] = pass[5][4] = 10;
    28     pass[4][6] = pass[6][4] = 5;
    29     pass[5][6] = pass[6][5] = 10;
    30     pass[7][8] = pass[8][7] = 10;
    31     pass[7][9] = pass[9][7] = 8;
    32     pass[8][9] = pass[9][8] = 10;
    33     pass[1][4] = pass[4][1] = 10;
    34     pass[1][7] = pass[7][1] = 4;
    35     pass[4][7] = pass[7][4] = 10;
    36     pass[2][5] = pass[5][2] = 10;
    37     pass[2][8] = pass[8][2] = 5;
    38     pass[5][8] = pass[8][5] = 10;
    39     pass[3][6] = pass[6][3] = 10;
    40     pass[3][9] = pass[9][3] = 6;
    41     pass[6][9] = pass[9][6] = 10;
    42     pass[1][5] = pass[5][1] = 10;
    43     pass[1][9] = pass[9][1] = 5;
    44     pass[5][9] = pass[9][5] = 10;
    45     pass[2][6] = pass[6][2] = 10;
    46     pass[4][8] = pass[8][4] = 10;
    47     pass[5][9] = pass[5][9] = 10;
    48     pass[2][4] = pass[4][2] = 10;
    49     pass[3][5] = pass[5][3] = 10;
    50     pass[5][7] = pass[7][5] = 10;
    51     pass[3][7] = pass[7][3] = 5;
    52     pass[5][7] = pass[7][5] = 10;
    53     pass[6][8] = pass[8][6] = 10;
    54     pass[1][6] = pass[6][1] = 10;
    55     pass[3][4] = pass[4][3] = 10;
    56     pass[4][9] = pass[9][4] = 10;
    57     pass[6][7] = pass[7][6] = 10;
    58     pass[1][8] = pass[8][1] = 10;
    59     pass[3][8] = pass[8][3] = 10;
    60     pass[2][7] = pass[7][2] = 10;
    61     pass[2][9] = pass[9][2] = 10;
    62     pass[0][1] = pass[0][2] = 10;
    63     pass[0][3] = pass[0][4] = 10;
    64     pass[0][5] = pass[0][6] = 10;
    65     pass[0][7] = pass[0][8] = 10;
    66     pass[0][9] = 10;
    67     vis[10] = true;
    68 }
    69 int main() {
    70     init();
    71     int T;
    72     scanf("%d",&T);
    73     while(T--) {
    74         scanf("%d",&n);
    75         for(int i = tot = 0; i < n; ++i)
    76             scanf("%d",a+i);
    77         sort(a,a+n);
    78         dfs(0,0);
    79         printf("%d
    ",tot);
    80         for(int i = 0; i < tot; ++i) {
    81             for(int j = 0; j < n; ++j)
    82                 printf("%d%c",d[i][j],j + 1 == n?'
    ':' ');
    83         }
    84     }
    85     return 0;
    86 }
    View Code
  • 相关阅读:
    nginx 部署
    win 7 系统ie浏览器升级11版本后,f12功能不可用的问题
    selenium 调用键盘按键
    selenium + python 环境搭建
    解决word2013老是打开未响应情况
    win7 64位备份时, 无法启动服务,0x80070422
    个人学习网站收集
    矩形后旋转后顶点坐标的求解
    Acrobat_8_Pro_SC 激活老是提示你输入的授权码无效
    DLL用def定义文件来导出重载函数(转)
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4420048.html
Copyright © 2011-2022 走看看