Each point has 16 directions, one step or two steps in one Direction, DFS will be able to save the number found.
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 is set by joining points to a 3x3 matrix in a chosen order. The points of the matrix is 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 DRA Wing the pattern). And we call those points as active points.
- For every-consecutive points A and B in the pattern representation if the line segment connecting A and B passes thro Ugh some other points, these points must is in the sequence also and comes before A and B, otherwise the pattern would be I Nvalid.
- In the pattern representation we don ' t mention the same point more than once, even if the pattern would touch this point AG Ain through another valid segment, and each segment in the pattern must is going from a point to another point which the P Attern didn ' t touch before and it might go through some points which already appeared in the pattern.
Now n is given active points, you need to find the number of valid pattern locks formed from those active points.
Input
There is multiple test cases. The first line of input contains an integer indicating the number of the T 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 a 1, a 2, ... a n (1≤ a i ≤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.
The next m lines, each contains n integers, indicating an valid pattern lock sequence. The m sequences should is listed in lexicographical order.
Sample Input
131 2 3
Sample Output
41 2 32 1 32 3 13 2 1
Author:LIN, Xi
Source:The 15th Zhejiang University Programming Contest
Submit Status
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <set > #include <vector>using namespace std;set<int> st;vector<int> 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.inser T (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<n;i++) {int x; scanf ("%d", &x); Go[x]=true; Graph[xy[x][0]][xy[x][1]]=true; } for (int i=1;i<=9;i++) {if (go[i]==false) continue; Vis[i]=true; DFS (xy[i][0],xy[i][1],i,i,0); Vis[i]=false; } int sz=vi.size (); printf ("%d\n", SZ); Sort (Vi.begin (), Vi.end ()); for (int i=0;i<sz;i++) {output (vi[i]); }} return 0;}
ZOJ 3861 Valid Pattern Lock DFS