(stl)Valid Pattern Lock

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.

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

1
3
1 2 3

Sample Output

4
1 2 3
2 1 3
2 3 1
3 2 1

分析:只需保證當前點與前一點之間沒有未訪問的點。next_permutation()生成排列。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <set>
#include <vector>
using namespace std;
const int maxn = 10005;
typedef long long LL;
int a[15], N;
vector<int>ans;
bool vis[15];
int bew[10][10];
void init()
{
	memset(bew, 0, sizeof(bew));
	bew[1][3] = bew[3][1] = 2;
	bew[1][7] = bew[7][1] = 4;
	bew[7][9] = bew[9][7] = 8;
	bew[9][3] = bew[3][9] = 6;
	bew[1][9] = bew[9][1] = bew[3][7] = bew[7][3] = 5;
	bew[4][6] = bew[6][4] = bew[2][8] = bew[8][2] = 5;
}
bool solve()
{
	vis[a[0]] = true;
	for (int i = 1; i < N; i++)
	{
		if (vis[bew[a[i]][a[i - 1]]] == true || bew[a[i]][a[i - 1]] == 0)
		{
			vis[a[i]] = true;
			continue;
		}
		else return false;
	}
	return true;
}
bool Judge()
{
	int x = (a[0] - 1) / 3;
	int y = (a[0] - 1) % 3;
	memset(vis, false, sizeof(vis));
	if (solve()) return true;
	else return false;
}
int main()
{
	//freopen("e:\\input.txt","r",stdin);
	int T;
	scanf("%d", &T);
	init();
	while (T--)
	{
		scanf("%d", &N);
		for (int i = 0; i<N; i++)
			scanf("%d", &a[i]);
		sort(a, a + N);
		int num = 0;
		ans.clear();
		do{
			if (Judge())
			{
				for (int i = 0; i<N; i++)   ans.push_back(a[i]);
				num++;
			}
		} while (next_permutation(a, a + N));

		printf("%d\n", num);
		int p = 0;
		for (int i = 0; i<num; i++, p += N)
		{
			for (int j = 0; j<N - 1; j++)
				printf("%d ", ans[p + j]);
			printf("%d\n", ans[p + N - 1]);
		}
	}
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章