标签:
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:
Now you are given n active points, you need to find the number of valid pattern locks formed from those active points.
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.
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.
1 3 1 2 3
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\n",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?‘\n‘:‘ ‘); 88 } 89 } 90 } 91 return 0; 92 }
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4420048.html