ZOJ 3861 Valid Pattern Lock

Source: Internet
Author: User

 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
Test instructions: There are several cases of determining which phone locks can be drawn.
Very dissatisfied,
I didn't even get it.
Plainly, the rules are not clear.
In fact, as long as the number of sets of records to record all occurrences, and then a number can pass as long as the judge by its two number used to be good. As long as there are two numbers used, the number next to it can pass, mom.
A water DFS
#include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <iostream > #include <algorithm>using namespace std;int n,a[15];int ans[15];bool ca[15];bool used[15];int cnt;bool judge (        int Cur,int last) {if (cur==1) {if (last==7 && (!ca[4] | |!used[4])) return 0;        if (last==3 && (!ca[2] | |!used[2])) return 0;    if (last==9 && (!ca[5] | |!used[5])) return 0;    } if (cur==2) {if (last==8 && (!ca[5] | |!used[5])) return 0;        } if (cur==3) {if (Last==1 && (!ca[2] | |!used[2])) return 0;        if (last==7 && (!ca[5] | |!used[5])) return 0;    if (last==9 && (!ca[6] | |!used[6])) return 0;    } if (cur==4) {if (last==6 && (!ca[5] | |!used[5])) return 0;    } if (cur==6) {if (last==4 && (!ca[5] | |!used[5])) return 0;        } if (cur==7) {if (Last==1 && (!ca[4] | |!used[4])) return 0; if (last==3 && (!ca[5] | |!used[5])) return 0;    if (last==9 && (!ca[8] | |!used[8])) return 0;    } if (cur==8) {if (last==2 && (!ca[5] | |!used[5])) return 0;        } if (cur==9) {if (last==7 && (!ca[8] | |!used[8])) return 0;        if (last==3 && (!ca[6] | |!used[6])) return 0;    if (Last==1 && (!ca[5] | |!used[5])) return 0; } return 1;}    void dfs_cnt (int dep,int last) {if (dep==n) {cnt++;            } for (int i=1;i<=n;i++) {if (!used[a[i]] && judge (A[i],last)) {used[a[i]]=1;            Ans[dep]=a[i];            DFS_CNT (Dep+1,a[i]);        used[a[i]]=0; }}}void dfs_print (int dep,int last) {if (dep==n) {for (int i=0;i<n;i++) {if (!i) print            F ("%d", ans[i]);        else printf ("%d", ans[i]);    } printf ("\ n");        } for (int i=1;i<=n;i++) {if (!used[a[i]] && judge (A[i],last)) {    Used[a[i]]=1;            Ans[dep]=a[i];            Dfs_print (Dep+1,a[i]);        used[a[i]]=0;    }}}int Main () {int T;    scanf ("%d", &t);        while (t--) {scanf ("%d", &n);            for (int i=1;i<=9;i++) {used[i]=1;        ca[i]=0;            } for (int i=1;i<=n;i++) {scanf ("%d", &a[i]);            used[a[i]]=0;        Ca[a[i]]=1;        } sort (a+1,a+n+1);        cnt=0;        DFS_CNT (0,0);        printf ("%d\n", CNT);    Dfs_print (0,0); } return 0;}


ZOJ 3861 Valid Pattern Lock

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.