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
手机锁屏,共9个数,然后给出规定,只能用给定的n个数做密码,求不同的锁屏方法有几种
对于一个点可以走向其他相邻8个方向,对于中间有相互隔开的情况,例如1到3,如果2已经访问过,那么方案是可行的,输出所有锁屏方案路径
#include <iostream> #include <stdio.h> #include <string.h> #include <stack> #include <queue> #include <map> #include <set> #include <vector> #include <math.h> #include <algorithm> using namespace std; #define ls 2*i #define rs 2*i+1 #define up(i,x,y) for(i=x;i<=y;i++) #define down(i,x,y) for(i=x;i>=y;i--) #define mem(a,x) memset(a,x,sizeof(a)) #define w(a) while(a) #define LL long long const double pi = acos(-1.0); #define Len 100005 #define mod 1000000007 int t,a[15],b[15],c[15][15],f[15],n,ans[500000][15],len; void print() { int i; len++; for (i=1; i<=n; i++) ans[len][i]=f[i]; } void dfs(int l) { int i; if(l>n) print(); else { for(i = 1; i<=9; i++) { if ((a[i]==1) && (b[i]==1) && (a[c[i][f[l-1]]]==0) && (b[c[i][f[l-1]]]==1)) { a[i]=0; f[l]=i; dfs(l+1); a[i]=1; f[l]=0; } } } } int main() { int t,i,x,j; memset(c,0,sizeof(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; scanf("%d",&t); while (t>0) { t--; memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); b[0]=1; scanf("%d",&n); for (i=1; i<=n; i++) { scanf("%d",&x); a[x]=1; b[x]=1; } len=0; dfs(1); printf("%d\n",len); for (i=1; i<=len; i++) { for (j=1; j<n; j++) printf("%d ",ans[i][j]); printf("%d\n",ans[i][n]); } } return 0; }