ZOJ--(5464)Valid Pattern Lock

来源:互联网 发布:yes淘宝刷信用 编辑:程序博客网 时间:2024/05/18 03:02

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.

valid_pattern_lock

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 a1a2, … 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

131 2 3

Sample Output

41 2 32 1 32 3 13 2 1
就是给你一个屏幕锁, 然后叫你判断有几种输出方式,其中当两个点之间的那个点没有被走过时,那么时不能走的,这两个点,然后叫你按照从字典序小到大的方式输出。
我的想法是把那些不能直接到达的两个点保存在map数组中,然后每组都进行清零操作,然后若那个点被走过,那么map就标记为1,说明可以走,然后用next_permutation输出就好。
#include<stdio.h>#include<string.h>#include<algorithm>#include<math.h>using namespace std;int map1[11][11];int a[22]={0},book[11];int c[441111][15]={0};void init(){memset(map1,0,sizeof(map1));int i,j;for(i=1;i<=9;i++)for(j=1;j<=9;j++)map1[i][j]=1;map1[1][3]=0;map1[1][7]=0;map1[1][9]=0;map1[2][8]=0;map1[3][1]=0;map1[3][7]=0;map1[3][9]=0;map1[4][6]=0;map1[6][4]=0;map1[7][1]=0;map1[7][3]=0;map1[7][9]=0;map1[8][2]=0;map1[9][1]=0;map1[9][3]=0;map1[9][7]=0;}int main(){int i,j,k;int T,n;while(~scanf("%d",&T)){while(T--){memset(a,0,sizeof(a));  memset(c,0,sizeof(c));memset(book,0,sizeof(book));scanf("%d",&n);for(i=0;i<n;i++) scanf("%d",&a[i]);sort(a,a+n);k=0;init();int flag=1;book[a[0]]=1;#if 1for(i=0;i<n-1;i++){if(map1[a[i]][a[i+1]]==0){if(max(a[i],a[i+1])-min(a[i],a[i+1])==2){if(book[min(a[i],a[i+1])+1]) {map1[a[i]][a[i+1]]=1; book[a[i]]=1; book[a[i+1]]=1;}else {flag=0; break;}}else if(max(a[i],a[i+1])-min(a[i],a[i+1])==6){if(book[min(a[i],a[i+1])+3]) {map1[a[i]][a[i+1]]=1; book[a[i]]=1; book[a[i+1]]=1;}else {flag=0; break;}}else if(max(a[i],a[i+1])-min(a[i],a[i+1])==8){if(book[min(a[i],a[i+1])+4]) {map1[a[i]][a[i+1]]=1; book[a[i]]=1; book[a[i+1]]=1;}else {flag=0; break;}}else if(max(a[i],a[i+1])-min(a[i],a[i+1])==4){if(book[min(a[i],a[i+1])+2]) {map1[a[i]][a[i+1]]=1; book[a[i]]=1; book[a[i+1]]=1;}else {flag=0; break;}}else {flag=0; break;}}else {book[a[i]]=1; book[a[i+1]]=1;}if(!flag) break;}if(flag){k++;for(i=0;i<n;i++) c[k][i]=a[i];}#endif#if 1while(next_permutation(a,a+n)){memset(book,0,sizeof(book));init();book[a[0]]=1;int flag=1;//for(i=0;i<n;i++) printf("%d ",a[i]); puts("");for(i=0;i<n-1;i++){if(map1[a[i]][a[i+1]]==0){if(max(a[i],a[i+1])-min(a[i],a[i+1])==2){if(book[min(a[i],a[i+1])+1]) {map1[a[i]][a[i+1]]=1; book[a[i]]=1; book[a[i+1]]=1;}else {flag=0; break;}}else if(max(a[i],a[i+1])-min(a[i],a[i+1])==6){if(book[min(a[i],a[i+1])+3]) {map1[a[i]][a[i+1]]=1; book[a[i]]=1; book[a[i+1]]=1;}else {flag=0; break;}}else if(max(a[i],a[i+1])-min(a[i],a[i+1])==8){if(book[min(a[i],a[i+1])+4]) {map1[a[i]][a[i+1]]=1; book[a[i]]=1; book[a[i+1]]=1;}else {flag=0; break;}}else if(max(a[i],a[i+1])-min(a[i],a[i+1])==4){if(book[min(a[i],a[i+1])+2]) {map1[a[i]][a[i+1]]=1; book[a[i]]=1; book[a[i+1]]=1;}else {flag=0; break;}}else {flag=0; break;}}else {book[a[i]]=1; book[a[i+1]]=1;}if(!flag) break;}//printf("%d\n",flag);if(flag){k++;for(i=0;i<n;i++) c[k][i]=a[i];}else continue;}printf("%d\n",k);for(i=1;i<=k;i++){for(j=0;j<n;j++){if(j!=n-1) printf("%d ",c[i][j]);else printf("%d\n",c[i][j]);}}#endif}}}

这只是一种方法,还有一种和这个差不多,但是使用dfs来搜的。

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;//f中保存的是路径; int f[111],pos,cd[111],map1[11][11],vis[11];int a[11]={0},n;int s;void dfs(int x,int last,int pos){if(x==n+1){if(pos==0) s++;else if(pos==1){for(int i=1;i<=n;i++){if(i!=n) printf("%d ",f[i]);else printf("%d\n",f[i]);}}return;}for(int i=1;i<=n;i++){if(vis[a[i]]==0){if(a[i]==1&&last==3&&vis[2]==0) continue;if(a[i]==1&&last==7&&vis[4]==0) continue;if(a[i]==1&&last==9&&vis[5]==0) continue;if(a[i]==2&&last==8&&vis[5]==0) continue;if(a[i]==3&&last==1&&vis[2]==0) continue;if(a[i]==3&&last==7&&vis[5]==0) continue;if(a[i]==3&&last==9&&vis[6]==0) continue;if(a[i]==4&&last==6&&vis[5]==0) continue;if(a[i]==6&&last==4&&vis[5]==0) continue;if(a[i]==7&&last==1&&vis[4]==0) continue;if(a[i]==7&&last==9&&vis[8]==0) continue;if(a[i]==7&&last==3&&vis[5]==0) continue;if(a[i]==8&&last==2&&vis[5]==0) continue;if(a[i]==9&&last==1&&vis[5]==0) continue;if(a[i]==9&&last==3&&vis[6]==0) continue;if(a[i]==9&&last==7&&vis[8]==0) continue;vis[a[i]]=1;f[x]=a[i];dfs(x+1,a[i],pos);vis[a[i]]=0;}}}int main(){int T;while(~scanf("%d",&T)){while(T--){scanf("%d",&n);memset(a,0,sizeof(a));for(int i=1;i<=n;i++) scanf("%d",&a[i]);sort(a+1,a+1+n);s=0;dfs(1,0,0);printf("%d\n",s);dfs(1,0,1);}}}
多积累总是好的,加油,hades!

0 0
原创粉丝点击