标签:tin ack uri clu algo ons his point ready
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.
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.
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.
1 3 1 2 3
4 1 2 3 2 1 3 2 3 1 3 2 1
排序然后dfs。
代码:
#include <iostream> #include <cstdlib> #include <cstdio> #include <algorithm> using namespace std; int t,n; int num[10]; char line[20]; bool vis[10]; vector<string> ans; int mp[10][10]; void dfs(int k) { if(k >= n) { line[k * 2 - 1] = 0; ans.push_back(line); return; } for(int i = 0;i < n;i ++) { if(vis[num[i]] || k && mp[line[k * 2 - 2] - ‘0‘][num[i]] && !vis[mp[line[k * 2 - 2] - ‘0‘][num[i]]]) continue; line[k * 2] = num[i] + ‘0‘; line[k * 2 + 1] = ‘ ‘; vis[num[i]] = true; dfs(k + 1); vis[num[i]] = false; } } int main() { mp[1][3] = mp[3][1] = 2; mp[4][6] = mp[6][4] = mp[2][8] = mp[8][2] = mp[1][9] = mp[9][1] = mp[3][7] = mp[7][3] = 5; mp[7][9] = mp[9][7] = 8; mp[1][7] = mp[7][1] = 4; mp[3][9] = mp[9][3] = 6; scanf("%d",&t); while(t --) { ans.clear(); scanf("%d",&n); for(int i = 0;i < n;i ++) { scanf("%d",&num[i]); } sort(num,num + n); dfs(0); printf("%d\n",ans.size()); for(int i = 0;i < ans.size();i ++) { puts(ans[i].c_str()); } } }
标签:tin ack uri clu algo ons his point ready
原文地址:https://www.cnblogs.com/8023spz/p/10799981.html