ZOJ3861: Valid Pattern Lock (DFS)

Source: Internet
Author: User

ZOJ3861: Valid Pattern Lock (DFS)

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 givenNActive 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 integerTIndicating the number of test cases. For each test case:

    The first line contains an integerN(3 ≤N≤ 9), indicating the number of active points. The second line containsNDistinct integersA1,A2 ,...AN(1 ≤AI≤ 9) which denotes the identifier of the active points.

    Output

    For each test case, print a line containing an integerM, Indicating the number of valid pattern lock.

    In the nextMLines, each containsNIntegers, indicating an valid pattern lock sequence.MSequences shoshould be listed in lexicographical order.

    Sample Input
    131 2 3
    Sample Output
    41 2 32 1 32 3 13 2 1
    Cell phone lock screen, a total of 9 numbers, and then give the rule, only use the given n number as the password, there are several different lock Methods
    If a point can go to the other eight adjacent directions and the center is separated from each other, for example, 1 to 3. If 2 has already been accessed, the solution is feasible, output all lock screen solution paths
    #include 
       
         #include 
        
          #include 
         
           #include 
          
            #include 
           
             #include
            #include 
             
               #include 
              
                #include 
               
                 #include using namespace std; #define ls 2*i #define rs 2*i+1 #define up(i,x,y) for(i=x;i<=y;i++) #define down(i,x,y) for(i=x;i>=y;i--) #define mem(a,x) memset(a,x,sizeof(a)) #define w(a) while(a) #define LL long long const double pi = acos(-1.0); #define Len 100005 #define mod 1000000007 int t,a[15],b[15],c[15][15],f[15],n,ans[500000][15],len; void print() { int i; len++; for (i=1; i<=n; i++) ans[len][i]=f[i]; } void dfs(int l) { int i; if(l>n) print(); else { for(i = 1; i<=9; i++) { if ((a[i]==1) && (b[i]==1) && (a[c[i][f[l-1]]]==0) && (b[c[i][f[l-1]]]==1)) { a[i]=0; f[l]=i; dfs(l+1); a[i]=1; f[l]=0; } } } } int main() { int t,i,x,j; memset(c,0,sizeof(c)); c[1][3]=2,c[3][1]=2; c[1][7]=4,c[7][1]=4; c[1][9]=5,c[9][1]=5; c[2][8]=5,c[8][2]=5; c[3][9]=6,c[9][3]=6; c[3][7]=5,c[7][3]=5; c[4][6]=5,c[6][4]=5; c[7][9]=8,c[9][7]=8; scanf("%d",&t); while (t>0) { t--; memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); b[0]=1; scanf("%d",&n); for (i=1; i<=n; i++) { scanf("%d",&x); a[x]=1; b[x]=1; } len=0; dfs(1); printf("%d\n",len); for (i=1; i<=len; i++) { for (j=1; j
                
               
              
             
           
          
         
        
       

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.