Zoj 3861 Valid pattern Lock (depth first traversal DFS, full arrangement with restricted conditions)

Source: Internet
Author: User



Valid pattern Lock Time Limit: 2000MS Memory Limit:65536KB 64bit IO Format:%lld &%l Lu



Description

Pattern lock security is generally used in Android handsets instead of a password. The pattern lock can is set by joining points on a 3x3 matrix into 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 is represented using the sequence of points it ' s which G for the "the" "same order of the drawing" pattern. And we call those points as active points. For every two consecutive points A and B on 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 in the pattern.

Now for are given n active points, you are need to find the number of valid pattern locks formed active those.

Input

There are multiple test cases. The "a" of input contains an integer T indicating the number of test cases. For each test case:

The 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 a 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



The main effect of the topic:

Phone unlock, now tell you have those number, and then let you give all the possible unlock sequence, the results are output in dictionary order.

Restriction conditions:

Each point can only go once

If there is a point in the middle of the two point, then the two dots must be connected to one another.


Analysis thought:

Since to the dictionary output, may be to find the full arrangement of N, just put it does not meet the conditions of the removal of it, so, recursive search solution for the idea of the whole arrangement, but need to two points in the middle of that point out, and then the two point of the key to judge.

Here I use a two-dimensional array to represent, Path[x][y] represents the number of keys between x and Y, and if x and Y are adjacent, then assign him to 0, or 0 to indicate that the two are adjacent. It's just a good time to judge the search.



Attached code:

#include <iostream> #include <cstring> #include <cstdio> #include <string> #include <
Algorithm> using namespace std;
int a[15];
int n;
int visit[15];
int path[15][15];
int ans[15];
int ans_n;
int ans[500000][10];
		void Dfs (int x, int t) {if (t = = N) {for (int i = 0;i < n;i++) {Ans[ans_n][i] = ans[i];
	} Ans_n + +;
			else {for (int i = 0;i < n;i++) {if (!visit[a[i]] && Visit[path[x][a[i]])//judgment X to A[i] can not row
				{Ans[t] = A[i];
				Visit[a[i]] = 1;
				DFS (a[i],t + 1);
			Visit[a[i]] = 0;
	int main () {//freopen ("OUT.txt", "w", stdout);
	memset (path,0,sizeof (path));       PATH[1][3] = 2;path[3][1] = 2;
	Indicates the key between each of the two keys path[1][7] = 4;path[7][1] = 4;
	PATH[4][6] = 5;path[6][4] = 5;
	PATH[7][9] = 8;path[9][7] = 8;
	PATH[3][9] = 6;path[9][3] = 6;
	PATH[2][8] = 5;path[8][2] = 5;
	PATH[1][9] = 5;path[9][1] = 5;
	PATH[3][7] = 5;path[7][3] = 5;
	int t;
	CIN >> T;
		while (t--) {ans_n = 0; ScANF ("%d", &n);
		for (int i = 0;i < n;i++) {scanf ("%d", &a[i]);
		Sort (a,a + N);
		memset (visit,0,sizeof (visit));
			for (int i = 0;i < n;i++) {Visit[a[i]] = 1;
			Visit[0] = 1;
			Ans[0] = A[i];
			DFS (a[i],1);
		Visit[a[i]] = 0;
		} cout << ans_n << Endl;            for (int i = 0;i < ans_n;i++) {for (int j = 0;j < n;j++) {if (J > 0) printf ("");
			Because the result is more, output to use C language output, if use C + + output, will timeout printf ("%d", ans[i][j]);
		printf ("\ n");
} return 0;  }


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.