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
#include#include #include #include #include #include using namespace std; set st; vector vi; bool vis[20]; int n; bool go[30]; bool graph[5][5]; int XY[10][2]={{0,0},{1,1},{1,2},{1,3},{2,1},{2,2},{2,3},{3,1},{3,2},{3,3}}; int TU[4][4]= { 0,0,0,0, 0,1,2,3, 0,4,5,6, 0,7,8,9 }; const int dir_x[16]={-1,-1,-1,0,0,1,1,1,-1,-1,1,1,-2,-2,2,2}; const int dir_y[16]={-1,0,1,-1,1,-1,0,1,-2,2,-2,2,-1,1,-1,1}; bool isIn(int a,int b) { if((a<=0||a>3)||(b<=0||b>3)) return false; return true; } int as[10]; void output(int x) { int na=0; while(x) { as[na++]=x%10; x/=10; } for(int i=na-1;i>=0;i--) printf(%d%c,as[i],(i==0)?10:32); } void dfs(int x,int y,int u,int nb,int deep) { if(deep==n-1) { if(st.count(nb)==0) { st.insert(nb); vi.push_back(nb); } return ; } for(int i=0;i<16;i++) { for(int j=1;j<=2;j++) { if(j==1) { int nx=x+dir_x[i],ny=y+dir_y[i]; if(isIn(nx,ny)==false) continue; int v=TU[nx][ny]; if(go[v]==false) continue; if(vis[v]==true) continue; vis[v]=true; dfs(nx,ny,v,nb*10+v,deep+1); vis[v]=false; } else if(j==2) { int nx=x+dir_x[i]*2,ny=y+dir_y[i]*2; if(isIn(nx,ny)==false) continue; /// check mid int xx=x+dir_x[i],yy=y+dir_y[i]; int tv=TU[xx][yy],vv=TU[nx][ny]; if(vis[tv]==false) continue; if(go[tv]==false) continue; if(vis[vv]==true) continue; if(go[vv]==false) continue; vis[vv]=true; dfs(nx,ny,vv,nb*10+vv,deep+1); vis[vv]=false; } } } } int main() { int T_T; scanf(%d,&T_T); while(T_T--) { scanf(%d,&n); memset(graph,false,sizeof(graph)); memset(go,false,sizeof(go)); st.clear(); vi.clear(); for(int i=0;i