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:
- 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 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.
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
1
3
1 2 3
Sample Output
4
1 2 3
2 1 3
2 3 1
3 2 1
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string.h>
#include <algorithm>
#include <map>
using namespace std;
int a[10];
int d[10];
int n;
int vis [10];
int sum;
int ans[200000][10]; //第一处错误 。。数组开成10W 而且居然有答案输出。。但是和20W的答案不同。n=9时 前者4W+ 后者14W
int cur;
int check(int x,int next)
{
if(x==1){
if(next==3)
if(vis[2]==1)
return 1;
if(next==7)
if(vis[4]==1)
return 1;
if(next==9)
if(vis[5]==1)
return 1;
if(next==3)
return 0;
if(next==7)
return 0;
if(next==9)
return 0;
return 1;
}
if(x==3){
if(next==1)
if(vis[2]==1)
return 1;
if(next==7)
if(vis[5]==1)
return 1;
if(next==9)
if(vis[6]==1)
return 1;
if(next==1)
return 0;
if(next==7)
return 0;
if(next==9)
return 0;
return 1;
}
if(x==7){
if(next==1)
if(vis[4]==1)
return 1;
if(next==3)
if(vis[5]==1)
return 1;
if(next==9)
if(vis[8]==1)
return 1;
if(next==1)
return 0;
if(next==3)
return 0;
if(next==9)
return 0;
return 1;
}
if(x==9){
if(next==1)
if(vis[5]==1)
return 1;
if(next==3)
if(vis[6]==1)
return 1;
if(next==7)
if(vis[8]==1)
return 1;
if(next==1)
return 0;
if(next==3)
return 0;
if(next==7)
return 0;
return 1;
}
if(x==2){
if(next==8)
if (vis[5])
return 1;
if(next==8)
return 0;
return 1;
}
if(x==8){
if(next==2)
if (vis[5])
return 1;
if(next==2)
return 0;
return 1;
}
if(x==4){
if(next==6)
if (vis[5])
return 1;
if(next==6)
return 0;
return 1;
}
if(x==6){
if(next==4)
if (vis[5])
return 1;
if(next==4)
return 0;
return 1;
}
return 1;
}
void dfs(int cur,int x)
{
if(cur==n)
{
for(int i=1;i<=n;i++)
ans[sum][i]=d[i];
sum++;
return ;
}
d[cur]=x;
for(int j=1;j<=n;j++){
if(!vis[a[j]])
{
if(check(x,a[j]))
{
d[++cur]=a[j];
vis[a[j]]=1;
dfs(cur,a[j]);
/* if(cur==n)
{
return ;
}*/
vis[a[j]]=0;
cur--;
}
}
}
}
int main()
{
int t,i,j;
cin>>t;
// t=1;
while (t--){
sum=0;
//memset(ans,0,sizeof(ans)); //不算错误 没必要memset
cin>>n;
for(i=1;i<=n;i++){
cin>>a[i];
}
/* n=8;
for (i=1;i<=n;i++)
{
a[i]=i;
}
*/
sort(a+1,a+1+n); //比赛时没看到要字典序输出
for(i=1;i<=n;i++){
memset(vis,0,sizeof(vis));
memset(d,0,sizeof(d));
vis[a[i]]=1;
dfs(1,a[i]);
}
cout<<sum<<endl;
for(i=0;i<sum;i++){
for(j=1;j<n;j++)
{
printf("%d ",ans[i][j]);//第二处错误 当时用了COUT 造成超时。。。。
}
printf("%d\n",ans[i][n]);
}
}
return 0;
}