码迷,mamicode.com
首页 > 其他好文 > 详细

解题报告 之 ZOJ3861 Valid Pattern Lock

时间:2015-05-19 10:52:44      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:zoj3861   valid pattern lock   搜索   dfs   acm   

解题报告 之 ZOJ3861 Valid Pattern Lock


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.

技术分享

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个点,问你有几种解锁图案?图案要求两个点中间如果有点则不能直接跨过,除非这个中间点已经走过了。(所以这里要明确,如果要以某个已经走过的点作为中转点,必须要跨过这个中转点,而不能用它作为走折线的中间点。比如1->2->3->6->4就是一种不合法的走法。因为6是不能经过2走折现到4的)。

分析:这个题一开始是dfs没错,生成所有可能的解法,复杂度O(n!)但因为n很小所以可以接受。比较难的在于判断这个序列是否可走,判断一个序列是否成立就是依次判断前后两个点是否能走。首先从第一个点开始,如果第二个点可以直接到达就算过,如果不能直接到达则看看有哪些中转站可以用(这个点存在且已经走过),再看看这个中转站能否到达目标节点,能到达就算过,直到判断完整个序列都合法就加入答案。

上代码:
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;

int ex[10];   //ex[i]==1表示按键 i 存在
int vis[10];   //vis表示按键已经走过
int g[10][10];  //g[i][j]表示i不能直接到j
int jump[10][10][10]; // i 经过 j 可以到 k
int str[400000][10];   //答案数组
int ans = 0;
int n;

int check( int now[] )
{

	int flag = 1;
	memset( vis, 0, sizeof vis );
	vis[now[1]] = 1;
	for(int i = 1; i < n; i++)
	{
		
		int f = false;
		if(!g[now[i]][now[i + 1]])
		{
			vis[now[i + 1]] = 1;
			continue;
		}
		
		for(int j = 1; j <= 9; j++)
		{
			if(vis[j] && jump[now[i]][j][now[i + 1]])
			{
				vis[now[i + 1]] = 1;
				f = true;
				break;
			}
		}
		if(f)continue;
		flag = false;
		break;
	}
	return flag;
}

void dfs( int dp, int num,int now[] )
{

	if(dp == n)
	{
		now[dp] = num;
		if(check( now ) == 1)
		{
			for(int i = 1; i <= n; i++)
				str[ans][i] = now[i];
			ans++;
		}
		return;
	}
	now[dp]=  num ;
	ex[num] = 0;
	for(int i = 1; i <= 9; i++)
	{
		if(ex[i])
			dfs( dp + 1, i, now );
	}
	ex[num] = 1;
}

int main()
{
	//freopen( "t.txt", "w", stdout );
	int kase;
	scanf( "%d", &kase );

	memset( g, 0, sizeof g );
	memset( vis, 0, sizeof vis );
	memset( jump, 0, sizeof jump );
	g[1][3] = g[1][9] = g[1][7] = 1;
	g[2][8] = 1;
	g[3][1] = g[3][9] = g[3][7] = 1;
	g[4][6] = 1;
	g[6][4] = 1;
	g[7][1] = g[7][3] = g[7][9] = 1;
	g[8][2] = 1;
	g[9][1] = g[9][3] = g[9][7] = 1;

	jump[1][2][3] = jump[1][4][7] = jump[1][5][9] = 1;
	jump[2][5][8] = 1;
	jump[3][6][9] = jump[3][2][1] = jump[3][5][7] = 1;
	jump[4][5][6] = 1;
	jump[6][5][4] = 1;
	jump[7][4][1] = jump[7][8][9] = jump[7][5][3] = 1;
	jump[8][5][2] = 1;
	jump[9][8][7] = jump[9][5][1] = jump[9][6][3] = 1;
	while(kase--)
	{
		ans = 0;
		scanf( "%d", &n );
		memset( ex, 0, sizeof ex );

		int tem;
		for(int i = 1; i <= n; i++)
		{
			scanf( "%d", &tem );
			ex[tem] = 1;
		}
		int t[10];
		dfs( 0, 0, t );
		printf( "%d\n", ans );
		//system( "pause" );
		for(int i = 0; i < ans; i++)
		{
			for(int j = 1; j < n; j++)
				printf( "%d ", str[i][j] );
			printf( "%d\n", str[i][n] );
		}
	}
	return 0;

}

对自己狠一点,只有自己强大了,才有资格嘲笑那些讨厌的人。

解题报告 之 ZOJ3861 Valid Pattern Lock

标签:zoj3861   valid pattern lock   搜索   dfs   acm   

原文地址:http://blog.csdn.net/maxichu/article/details/45827125

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!