ZOJ problem Set-3861 (DFS + subset Generation + conditional full-array generation)

Source: Internet
Author: User
Tags cmath

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 is given n 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 T indicating the number of the 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.

The next m lines, each contains n integers, and indicating an valid pattern lock sequence. The M sequences should is 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
Test instructions is, matching the condition of the mobile phone unlocked trajectory, adjacent points can be directly connected to each other, but if there is a point between two points, regardless of whether this point to you, as long as this point is not used, then the two points can not be connected through this point, such as to you 1 3 5, is not to appear 1 3 5 or 3 1 5 This sequence, because 2 this point exists, although not to you but he is not used, so 1 3 or 3 1 will inevitably go through 2,

#include <cstdio> #include <algorithm> #include <cmath> #include <cmath> #include <bits/stdc    ++.h>using namespace Std;template<class t>inline T Read (t&x) {char C;    while ((C=getchar ()) <=32) if (c==eof) return 0;    BOOL Ok=false;    if (c== '-') Ok=true,c=getchar ();    for (x=0; c>32; C=getchar ()) x=x*10+c-' 0 ';    if (OK) x=-x; return 1;} Template<class t> inline T read_ (t&x,t&y) {return read (x) &&read (y);} Template<class t> inline T read__ (t&x,t&y,t&z) {return read (x) &&read (y) &&read (z);}    Template<class t> inline void Write (T x) {if (x<0) Putchar ('-'), x=-x;    if (x<10) putchar (x+ ' 0 '); else write (X/10), Putchar (x%10+ ' 0 ');}    Template<class t>inline void Writeln (T x) {write (x); Putchar (' \ n ');} -------ZCC IO template------const int MAXN=11;CONST double inf=999999999; #define Lson (rt<<1), L,m#define Rson ( rt<<1|1), M+1,r#define M ((l+r) >>1) #define for (i, t,n) for (int i= (t); i<= (n); i++)//typedef long long ll;typedef double db;typedef pair<int,int> P; #define BUG Prin TF ("---\ n"); #define MOD 10007int num[maxn];int ans[maxn];int myans[140705][maxn];bool used[maxn];bool pan (int ppre,int Pre) {if (ppre==1&&pre==3| | pre==1&&ppre==3) && (num[2]&&!used[2]| |!    NUM[2]) return true; if (ppre==4&&pre==6| | pre==4&&ppre==6) && (num[5]&&!used[5]| |!    NUM[5]) return true; if (ppre==7&&pre==9| | pre==7&&ppre==9) && (num[8]&&!used[8]| |!    NUM[8]) return true; if (ppre==1&&pre==7| | pre==1&&ppre==7) && (num[4]&&!used[4]| |!    NUM[4]) return true; if (ppre==2&&pre==8| |pre==2&&ppre==8) && (num[5]&&!used[5]| |!    NUM[5]) return true; if (ppre==3&&pre==9| | pre==3&&ppre==9) && (num[6]&&!used[6]| |!    NUM[6]) return true; if (ppre==1&&pre==9| | PRE==1&AMP;&AMP;PPRE==9) &AMP;&AMP; (num[5]&&!used[5]| |!    NUM[5]) return true; if (ppre==3&&pre==7| | pre==3&&ppre==7) && (num[5]&&!used[5]| |!    NUM[5]) return true; return false;}        int kk;void Solve (int cnt,int n) {if (cnt==n) {for (i,0,n-1) {myans[kk][i]=ans[i];        }kk++;    return; } for (i,1,9) {if (Num[i]&&!used[i]) {if (Cnt>=1&&pan (ans[cnt-1],i)) Conti            Nue            Used[i]=true;            Ans[cnt]=i;            Solve (cnt+1,n);        Used[i]=false;    }}}int Main () {//#ifndef Online_judge//freopen ("In.txt", "R", stdin);    Freopen ("Zccccc.txt", "w", stdout);    #endif//Online_judge int n,m,i,j,t,k;    int T;    Read (T);        while (t--) {read (n);        memset (num,0,sizeof (num));        memset (ans,0,sizeof (ans));        memset (used,0,sizeof (used));        for (I,1,n) read (k), num[k]=1;        kk=0;        Solve (0,n); Writeln (KK); for (Ii,0,kk-1) for (J,0,n-1) printf ("%d%c", myans[ii][j],j==n-1?    \ n ': '); } return 0;}


ZOJ problem Set-3861 (DFS + subset Generation + conditional full-array generation)

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.