ZOJ 3861 Valid Pattern Lock(深度優先遍歷dfs,有限制條件的全排列)



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

1
3
1 2 3

Sample Output

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



題目大意:

手機解鎖,現在給你說 有 那幾個 號 ,然後 讓你給出所有的可能解鎖順序,結果按字典序輸出。

限制條件:

● 每個點只能走一次

●  如果兩個點中間有一個點,那麼,必須中間這個點走過的情況下才能將這兩個點連到一塊


分析思想:

既然要字典序輸出,可能爲是求n的全排列,只是把其中不符合條件的去掉而已,所以,遞歸搜索求解全排列的思想求解,不過需要將兩點中間那個點表示出來,然後對兩點的那個鍵進行判斷。

這裏 我用了  一個二維數組表示出來,path[x][y]  表示 x號 和 y號  中間的鍵是 幾號,如果x 和 y 是相鄰的,那麼就把他賦值成 0 ,也就是 0 表示兩個 是相鄰的。這樣搜索的時候判斷一下就行了



附上代碼:

#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]]])   //  判斷 x 到 a[i] 能劃不能
			{
				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;       //  表示出 每兩個按鍵之間的那個按鍵
	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(" ");            //  由於結果比較多,輸出要用 c 語言輸出,若用 c++  輸出,會超時
				printf("%d",Ans[i][j]);
			}
			printf("\n");
		}
	}
	return 0;
} 


發佈了106 篇原創文章 · 獲贊 31 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章