ZOJ-3861 DFS+回溯

来源:互联网 发布:罗德里格矩阵 编辑:程序博客网 时间:2024/05/09 15:19

B - Valid Pattern Lock
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu


Description

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

需要注意的是这种情况是合法的:1-6(就是1可以直接到6,而不经过其它点),1-8···(或许是我个人使用手机锁的习惯造成)


#include <iostream>
#include<vector>
#include<string>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<deque>
#include<cstring>
#include<map>
using namespace std;
int path[12][12];
bool vs[12];
bool active[12];
int a[12];
int n;
int total;
vector<int>vec;
int ans[200010][10];
void init()
{
memset(path, 0, sizeof(path));
path[1][3] = path[3][1] = 2;
path[1][7] = path[7][1] = 4;
path[1][9] = path[9][1] = 5;
path[2][8] = path[8][2] = 5;
path[3][9] = path[9][3] = 6;
path[3][7] = path[7][3] = 5;
path[4][6] = path[6][4] = 5;
path[7][9] = path[9][7] = 8;
}
void dfs(int num)
{
if (num == n)
{
for (int i = 0; i<n; i++)
{
ans[total][i] = vec[i];
}
total++;
return;
}
for (int j = 1; j <= 9; j++)
{
if (active[j] && !vs[j] && vs[path[vec[num - 1]][j]])
{
vs[j] = 1;
vec.push_back(j);
dfs(num + 1);
vec.erase(vec.begin() + num);
vs[j] = 0;
}
}
return;
}
int main()
{
int tes;
scanf("%d", &tes);
init();
while (tes--)
{
scanf("%d", &n);
memset(vs, 0, sizeof(vs));
memset(active, 0, sizeof(active));
active[0] = 1;
vs[0] = 1;
total = 0;
vec.clear();
for (int i = 0; i<n; i++)
{
scanf("%d", &a[i]);
active[a[i]] = 1;
}
sort(a, a + n);
for (int i = 0; i<n; i++)
{
vec.push_back(a[i]);
vs[a[i]] = 1;
dfs(1);
vs[a[i]] = 0;
vec.clear();
}
printf("%d\n", total);
for (int i = 0; i<total; i++)
{
for (int j = 0; j<n; j++)
{
if (j)printf(" %d", ans[i][j]);
else printf("%d", ans[i][0]);
}
printf("\n");
}
}
}



0 0
原创粉丝点击