ZOJ Problem Set - 3861 ( DFS + 子集生成 + 有条件的全排列生成 )

来源:互联网 发布:巴黎地铁游客数据 编辑:程序博客网 时间:2024/05/15 11:51

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 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
题意是,符合条件的手机解锁的轨迹,相邻的点之间是可以直接相连接的,但是如果两点之间存在一个点,不管这个点是否给你,只要这个点是没有用过的,那么这两个点是不能经过越过这个点连在一起的,比如给你 1 3 5 ,是不能出现1 3 5 或者 3 1 5 这种序列的,因为2 这个点存在,虽然没给你但是他是没有用过的 ,所以1 3 或者3 1 必然会经过 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 printf("---\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&&ppre==9)&&(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))continue;            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;}


 

1 0
原创粉丝点击