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
题目描述 :
读题读了半天,没读懂,真对自己无语,能不能灵活点,孩子。给你一张图,如图所示,然后给你一些点,将这些点相连,中途不经过其他的点可直接相连,如果经过其他的点的话,那么这些点之前已经
被连过(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.)。
想法是深搜,但是有两个点需要注意,下一步的选择,怎么去处理。由于定义的状态是位置,所以无脑地枚举了24个位置,并且判断相连的点有无经过其他的点,也是枚举。
太无脑了,我们可以把数字定义为状态(其实想到了跟用深搜生成排列差不多,可是受到图的干扰,以后要想清楚,抽象好问题),然后枚举下一个可以到达的点。
由于是要按照字典序输出,先将a数组排序。接下来就是对(然后给你一些点,将这些点相连,中途不经过其他的点可直接相连,如果经过其他的点的话,那么这些点之前已经
被连过)这句话的处理。既然要能相连,要么不经过其它点,要么经过已经访问过的点。我们可以将两种情形转化为一种情况,能相连的不经过其他点的,中间给他加上已经
访问过的点,那么所有能相连的点中间都要有访问过的点,以后每次只需判断这种情况就可以。
通过预处理,将要判断的情况减少。
最后再说一句,第一次遇到这种格式错误,注意一下。
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #define maxn 150000 using namespace std; int n; int a[15][15]; int visit[15]; int sum; int _result[maxn][15]; int result[maxn]; int b[15]; void init() { memset(_result,0,sizeof(_result)); sum=0; } void dfs(int x,int _add) { if(_add==n) { sum+=1; for(int i=0;i<n;i++) _result[sum][i]=result[i]; return ; } for(int i=1;i<=n;i++) { if(visit[ a[x][b[i]] ]==1 && visit[b[i]]==0) { visit[b[i]]=1; // _visit[b[i]]=1; result[_add]=b[i]; dfs(b[i],_add+1); visit[b[i]]=0; // _visit[b[i]]=0; } } } int main() { /* for(int i=0;i<24;i++) { for(int j=0;j<2;j++) printf("%d ",walk[i][j]); printf(" "); }*/ for(int i=0;i<15;i++) for(int j=0;j<15;j++) a[i][j]=10; memset(visit,0,sizeof(visit)); visit[10]=1; a[1][3]=2; a[3][1]=2; a[4][6]=5; a[6][4]=5; a[7][9]=8; a[9][7]=8; a[1][7]=4; a[7][1]=4; a[2][8]=5; a[8][2]=5; a[3][9]=6; a[9][3]=6; a[1][9]=5; a[9][1]=5; a[3][7]=5; a[7][3]=5; int T; scanf("%d",&T); while(T--) { init(); scanf("%d",&n); int data; for(int i=1;i<=n;i++) { scanf("%d",&b[i]); } sort(b+1,b+n+1); for(int i=1;i<=n;i++) { result[0]=b[i]; visit[b[i]]=1; dfs(b[i],1); visit[b[i]]=0; } printf("%d ",sum); for(int i=1;i<=sum;i++) { printf("%d",_result[i][0]); for(int j=1;j<n;j++) printf(" %d",_result[i][j]); printf(" "); } } return 0; }
下面是没有预处理的减少判断的,而且傻逼了的想法。
不过可以进行优化,判断下一个状态与当前状态之间有无其他点,
如果没有可以访问下一个点,如果有,判断是否被访问。而判断是否
被访问,可以通过预处理,判断只需O(1)时间,极大简化代码,缩短时间。
在过程中判断,情况太多。
#include <iostream>
#include <cstdio> #include <cstring> #define maxn 100000 using namespace std; int a[15][15];; int n; int visit[15][15]; int sum; int _result[maxn][15]; int result[maxn]; int t1,t2; int walk[50][2]={ -2,-2, -2,-1, -2,0, -2,1, -2,2, -1,-2, -1,-1, -1,0, -1,1, -1,2, 0,-2, 0,-1, 0,1, 0,2, 1,-2, 1,-1, 1,0, 1,1, 1,2, 2,-2, 2,-1, 2,0, 2,1, 2,2 }; void init() { memset(visit,0,sizeof(visit)); for(int i=0;i<15;i++) for(int j=0;j<15;j++) a[i][j]=-1; memset(_result,0,sizeof(_result)); sum=0; t1=0; t2=0; } void dfs(int x,int y,int _add) { if(_add==n) { sum+=1; for(int i=0;i<n;i++) _result[sum][i]=result[i]; return ; } int xx,yy; for(int i=0;i<24;i++) { xx=x+walk[i][0]; yy=y+walk[i][1]; if(xx>=1 && xx<=3 && yy>=1 && yy<=3 && a[xx][yy]!=-1) { if(x==xx && yy-y==2) { if(visit[x][y+1]==0) continue ; else { result[_add]=a[xx][yy]; dfs(xx,yy,_add+1); } } else if(x==xx && y-yy==2) { if(visit[x][y-1]==0) continue ; else { result[_add]=a[xx][yy]; dfs(xx,yy,_add+1); } } else if(y==yy && xx-x==2) { if(visit[x][x+1]==0) continue ; else { result[_add]=a[xx][yy]; dfs(xx,yy,_add+1); } } else if(y==yy && x-xx==2) { if(visit[x][x-1]==0) continue ; else { result[_add]=a[xx][yy]; dfs(xx,yy,_add+1); } } else if( (x==1 && y==1 && xx==3 && yy==3) || (x==3 && y==3 && xx==1 && yy==1) || (x==3 && y==1 && xx==1 && yy==3) || (x==1 && yy==3 && xx==3 && yy==1) ) { if(visit[2][2]==0) continue ; else { result[_add]=a[xx][yy]; dfs(xx,yy,_add+1); } } else if(visit[xx][yy]==0) { visit[xx][yy]=1; result[_add]=a[xx][yy]; dfs(xx,yy,_add+1); visit[xx][yy]=0; } } } } int main() { /* for(int i=0;i<24;i++) { for(int j=0;j<2;j++) printf("%d ",walk[i][j]); printf(" "); }*/ int T; scanf("%d",&T); while(T--) { init(); scanf("%d",&n); int data; for(int i=1;i<=n;i++) { scanf("%d",&data); if(data==1) a[1][1]=data; if(data==2) a[1][2]=data; if(data==3) a[1][3]=3; if(data==4) a[2][1]=4; if(data==5) a[2][2]=5; if(data==6) a[2][3]=6; if(data==7) a[3][1]=7; if(data==8) a[3][2]=8; if(data==9) a[3][3]=9; } for(int i=1;i<=3;i++) for(int j=1;j<=3;j++) { if(a[i][j]!=-1) { result[0]=a[i][j]; visit[i][j]=1; dfs(i,j,1); visit[i][j]=0; } } printf("%d ",sum); for(int i=1;i<=sum;i++) { printf("%d",_result[i][0]); for(int j=1;j<n;j++) printf(" %d",_result[i][j]); printf(" "); } } return 0; }