解题报告 之 ZOJ3861 Valid Pattern Lock

解题报告 之 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;

}

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

时间: 2024-10-31 15:44:35

解题报告 之 ZOJ3861 Valid Pattern Lock的相关文章

ZOJ3861:Valid Pattern Lock(DFS)

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 u

ZOJ3861 Valid Pattern Lock

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3861 题意:给出一组数,要求这组数在解锁的界面可能的滑动序列. 思路:按照是否能够直接到达建图,如1可以直接到2,但是1不能直接到3,因为中间必须经过一个2. 要注意的假如2已结访问过,那么1就可以直接到2. 建图DFS,图要更新. 1 #include <iostream> 2 #include <cstring> 3 #include <

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 regis

2016 省热身赛 Valid Pattern Lock

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 matr

ZOJ 3861 Valid Pattern Lock DFS

每个点有16种方向,向某个方向走一步或者两步,dfs把找到的数都存下来就可以了..... 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

ZOJ - 3861 Valid Pattern Lock(dfs或其他,两种解法)

Valid Pattern Lock Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %llu Submit Status Description Pattern lock security is generally used in Android handsets instead of a password. The pattern lock can be set by joining points on

ZOJ14选拔赛——DFS+暴力——Valid Pattern Lock

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 u

DFS+模拟 ZOJ 3861 Valid Pattern Lock

题目传送门 1 /* 2 题意:手机划屏解锁,一笔连通所有数字,输出所有可能的路径: 3 DFS:全排列 + ok () 判断函数,去除一些不可能连通的点:) 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <algorithm> 8 #include <cstring> 9 #include <string> 10 #include <map> 11 #includ

zoj 3861 Valid Pattern Lock(全排列 模拟)

#include<cstdio> #include<iostream> #include<cstring> #include<cmath> #include<stdlib.h> #include<queue> #include<stack> #include<vector> #include<algorithm> #define LL long long using namespace std; i