标签:zoj3861 valid pattern lock 搜索 dfs acm
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:
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 a1, a2, …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
#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