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 Tindicating 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.
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
1231
3
1
2
3
Sample Output
123454
1
2
3
2
1
3
2
3
1
3
2
1
/*
题目:B Valid Pattern Lock
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5473
题意:给定几个数字 ,问能组成多少种手势密码,并按照字典序输出
思路:从小到大DFS
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int s[10]={0},a[10]={0},vis[10]={0},n,cnt,ans[150000][10]={0};
int no[8][2]={{1,3},{1,7},{1,9},{2,8},{3,7},{3,9},{4,6},{7,9} };
bool judge(int x,int y) //判定是否越过‘未访问数字’
{
if(x>y) swap(x,y);
for(int i=0;i<8;i++)
if(x==no[i][0]&&y==no[i][1]&&!vis[(x+y)/2]) return false;
return true;
}
void dfs(int x,int k)
{
if(k==n-1){
for(int i=0;i<n;i++) ans[cnt][i]=s[i];//ans储存最终结果
cnt++; //结果数+1
}
else for(int i=0; i<n; i++)
if(!vis[a[i]]&&judge(x,a[i])) //没有被访问过,而且不会越过‘未访问数字’
{
s[k+1]=a[i];
vis[a[i]]=1; //访问标记
dfs(a[i],k+1); //向下dfs
vis[a[i]]=0; //解除
}
}
void solve()
{
cnt=0;memset(vis,0,sizeof(vis));//初始化
for(int i=0; i<n; i++)
{
s[0]=a[i];
vis[a[i]]=1;
dfs(a[i],0);
vis[a[i]]=0;
}
}
int main()
{
int T;scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%d",&a[i]);
sort(a,a+n);//排序
solve();
printf("%d\n",cnt);
for(int i=0;i<cnt;i++){
for(int j=0;j<n-1;j++) printf("%d ",ans[i][j]);
printf("%d\n",ans[i][n-1]);
}
}
return 0;
}