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
#include <iostream>
#include <cstdio>
第二次写的代码
#include <cstdio> #include <cmath> #include <cstring> #include <string> #include <algorithm> #include <iostream> #include <queue> using namespace std; int n; int cnt,ok; int f[10]; int vis[10]; int tmp[10]; int ans[1000000][10]; int search(int x) { tmp[++cnt]=x; vis[x]=1; if (cnt==n) { int i; ok++; for (i=1;i<=n;i++) { ans[ok][i]=tmp[i]; } vis[x]=0; cnt--; return 0; } if (x==1) { if (f[2]&&!vis[2]) search(2); if (f[3]&&!vis[3]) if (vis[2]) search(3); if (f[4]&&!vis[4]) search(4); if (f[5]&&!vis[5]) search(5); if (f[6]&&!vis[6]) search(6); if (f[7]&&!vis[7]) if (vis[4]) search(7); if (f[8]&&!vis[8]) search(8); if (f[9]&&!vis[9]) if (vis[5]) search(9); } if (x==2) { if (f[1]&&!vis[1]) search(1); if (f[3]&&!vis[3]) search(3); if (f[4]&&!vis[4]) search(4); if (f[5]&&!vis[5]) search(5); if (f[6]&&!vis[6]) search(6); if (f[7]&&!vis[7]) search(7); if (f[8]&&!vis[8]) if (vis[5]) search(8); if (f[9]&&!vis[9]) search(9); } if (x==3) { if (f[1]&&!vis[1]) if (vis[2]) search(1); if (f[2]&&!vis[2]) search(2); if (f[4]&&!vis[4]) search(4); if (f[5]&&!vis[5]) search(5); if (f[6]&&!vis[6]) search(6); if (f[7]&&!vis[7]) if (vis[5]) search(7); if (f[8]&&!vis[8]) search(8); if (f[9]&&!vis[9]) if (vis[6]) search(9); } if (x==4) { if (f[1]&&!vis[1]) search(1); if (f[2]&&!vis[2]) search(2); if (f[3]&&!vis[3]) search(3); if (f[5]&&!vis[5]) search(5); if (f[6]&&!vis[6]) if (vis[5]) search(6); if (f[7]&&!vis[7]) search(7); if (f[8]&&!vis[8]) search(8); if (f[9]&&!vis[9]) search(9); } if (x==5) { if (f[1]&&!vis[1]) search(1); if (f[2]&&!vis[2]) search(2); if (f[3]&&!vis[3]) search(3); if (f[4]&&!vis[4]) search(4); if (f[6]&&!vis[6]) search(6); if (f[7]&&!vis[7]) search(7); if (f[8]&&!vis[8]) search(8); if (f[9]&&!vis[9]) search(9); } if (x==6) { if (f[1]&&!vis[1]) search(1); if (f[2]&&!vis[2]) search(2); if (f[3]&&!vis[3]) search(3); if (f[4]&&!vis[4]) if (vis[5]) search(4); if (f[5]&&!vis[5]) search(5); if (f[7]&&!vis[7]) search(7); if (f[8]&&!vis[8]) search(8); if (f[9]&&!vis[9]) search(9); } if (x==7) { if (f[1]&&!vis[1]) if (vis[4]) search(1); if (f[2]&&!vis[2]) search(2); if (f[3]&&!vis[3]) if (vis[5]) search(3); if (f[4]&&!vis[4]) search(4); if (f[5]&&!vis[5]) search(5); if (f[6]&&!vis[6]) search(6); if (f[8]&&!vis[8]) search(8); if (f[9]&&!vis[9]) if (vis[8]) search(9); } if (x==8) { if (f[1]&&!vis[1]) search(1); if (f[2]&&!vis[2]) if (vis[5]) search(2); if (f[3]&&!vis[3]) search(3); if (f[4]&&!vis[4]) search(4); if (f[5]&&!vis[5]) search(5); if (f[6]&&!vis[6]) search(6); if (f[7]&&!vis[7]) search(7); if (f[9]&&!vis[9]) search(9); } if (x==9) { if (f[1]&&!vis[1]) if (vis[5]) search(1); if (f[2]&&!vis[2]) search(2); if (f[3]&&!vis[3]) if (vis[6]) search(3); if (f[4]&&!vis[4]) search(4); if (f[5]&&!vis[5]) search(5); if (f[6]&&!vis[6]) search(6); if (f[7]&&!vis[7]) if (vis[8]) search(7); if (f[8]&&!vis[8]) search(8); } cnt--; vis[x]=0; return 0; } int main() { int i,j,k,t,m,c; scanf("%d",&t); for (k=1;k<=t;k++) { memset(f,0,sizeof(f)); scanf("%d",&n); for (i=1;i<=n;i++) { scanf("%d",&c); f[c]=1; } ok=0; for (i=1;i<=9;i++) { if (f[i]) { memset(vis,0,sizeof(vis));//是否选过 memset(tmp,0,sizeof(tmp));//临时答案 cnt=0; search(i); } } printf("%d\n",ok); for (i=1;i<=ok;i++) { for (j=1;j<=n;j++) { if (j!=1) printf(" "); printf("%d",ans[i][j]); } printf("\n"); } } return 0; }