码迷,mamicode.com
首页 > 其他好文 > 详细

Valid Pattern Lock

时间:2015-04-13 22:51:31      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

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.

技术分享

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

 

技术分享
  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<algorithm>
  4 int T ;
  5 int n , a[15] ;
  6 int b[15] ;
  7 bool vis[15] ;
  8 bool blog[15] ;
  9 bool flag = 0 ;
 10 int cnt = 0 ;
 11 int ans[362880 + 10][11] ;
 12 
 13 bool judge (int sx , int ex)
 14 {
 15     if (sx == 1 && ex == 3 || sx == 3 && ex == 1) {
 16         if (blog[2]) return true ;
 17         return false ;
 18     }
 19     else if (sx == 1 && ex == 7 || sx == 7 && ex == 1) {
 20         if (blog[4]) return true ;
 21         return false ;
 22     }
 23     else if (sx == 3 && ex == 9 || sx == 9 && ex == 1) {
 24         if (blog[6]) return true ;
 25         return false ;
 26     }
 27     else if (sx == 7 && ex == 9 || sx == 9 && ex == 7) {
 28         if (blog[8]) return true ;
 29         return false ;
 30     }
 31     else if (sx == 2 && ex == 8 || sx == 8 && ex == 2) {
 32         if (blog[5]) return true ;
 33         return false ;
 34     }
 35     else if (sx == 4 && ex == 6 || sx == 6 && ex == 4) {
 36         if (blog[5]) return true ;
 37         return false ;
 38     }
 39     else if (sx == 1 && ex == 9 || sx == 9 && ex == 1) {
 40         if (blog[5]) return true ;
 41         return false ;
 42     }
 43     else if (sx == 3 && ex == 7 || sx == 7 && ex == 3) {
 44         if (blog[5]) return true ;
 45         return false ;
 46     }
 47     return true ;
 48 }
 49 
 50 void dfs (int deep)
 51 {
 52     if (deep == n + 1 ) {
 53         memset (blog , 0 , sizeof(blog)) ;
 54         flag = 1 ;
 55         blog[b[1]] = 1 ;
 56         for (int i = 2 ; i <= n && flag; i++) {
 57             flag = judge (b[i - 1] , b[i]) ;
 58             blog[b[i]] = 1 ;
 59         }
 60         if (flag) {
 61             for (int i = 1 ; i <= n ; i++) {
 62                 ans[cnt][i] = b[i] ;
 63             }
 64             cnt ++ ;
 65         }
 66       /*  for (int i = 1 ; i <= n ; i++) {
 67             printf ("%d " , a[i]);
 68         }
 69         puts ("") ;*/
 70         return ;
 71     }
 72     for (int i = 1 ; i <= n ; i++) {
 73         if (vis[a[i]] == 0) {
 74             vis[a[i]] = 1 ;
 75             b[deep] = a[i] ;
 76             dfs (deep + 1) ;
 77             vis[a[i]] = 0 ;
 78         }
 79     }
 80 }
 81 
 82 int main ()
 83 {
 84    // freopen ("a.txt" , "r" , stdin ) ;
 85     scanf ("%d" , &T) ;
 86     while (T --) {
 87         scanf ("%d" , &n) ;
 88         for (int i = 1 ; i <= n ; i++) {
 89             scanf ("%d" , &a[i]);
 90         }
 91         cnt = 0 ;
 92         std::sort (a + 1 , a + n + 1 ) ;
 93         for (int i = 1 ; i <= n ; i++) {
 94             memset (vis , 0 , sizeof(vis)) ;
 95             vis[a[i]] = 1 ;
 96             b[1] = a[i] ;
 97             dfs (2) ;
 98         }
 99         printf ("%d\n" , cnt) ;
100         for (int i = 0 ; i < cnt ; i++) {
101             for (int j = 1 ; j <= n ; j++) {
102                 printf ("%d%c" , ans[i][j] , j == n ? \n :  ) ;
103             }
104         }
105     }
106     return 0 ;
107 }
View Code

 

Valid Pattern Lock

标签:

原文地址:http://www.cnblogs.com/get-an-AC-everyday/p/4423317.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!