ZOJ-3861 Valid Pattern Lock (dfs or others, two solutions)
Valid Pattern Lock
Time Limit:2000 MS |
|
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 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 givenNActive 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 integerTIndicating the number of test cases. For each test case:
The first line contains an integerN(3 ≤N≤ 9), indicating the number of active points. The second line containsNDistinct integersA1,A2 ,...AN(1 ≤AI≤ 9) which denotes the identifier of the active points.
Output
For each test case, print a line containing an integerM, Indicating the number of valid pattern lock.
In the nextMLines, each containsNIntegers, indicating an valid pattern lock sequence.MSequences shoshould be listed in lexicographical order.
Sample Input
131 2 3
Sample Output
41 2 32 1 32 3 13 2 1
The rule is the mobile phone gesture unlocking rule. An array is used to store numbers in the middle of two numbers for easy judgment. There are two ways to consider this question: dfs, one is to enumerate the arrangement of all n numbers to determine whether it is consistent. Dfs needs to record the path, and there are multiple public segments in the path, which is a bit difficult. Fortunately, I am wise to store the answer for an int, however, there are a lot of operations to process, and there are actually more operations than using a dfs. The second method is intuitive.
Paste the following two codes:
Dfs
# Include
# Include
# Include
# Include
# Include
Using namespace std; int mp [12] [12]; int n, kind, used [123], a [123], B [123]; int result [400000]; int ans; void init () {mp [1] [3] = 2; mp [3] [1] = 2; mp [1] [7] = 4; mp [7] [1] = 4; mp [1] [9] = 5; mp [9] [1] = 5; mp [2] [8] = 5; mp [8] [2] = 5; mp [3] [7] = 5; mp [7] [3] = 5; mp [3] [9] = 6; mp [9] [3] = 6; mp [4] [6] = 5; mp [6] [4] = 5; mp [7] [9] = 8; mp [9] [7] = 8;} void dfs (int cur, int x) {ans = ans * 10 + cur; if (x = n) {kind ++; result [kind] = ans; return;} for (int I = 0; I
> T; init (); while (t --) {scanf ("% d", & n); for (int I = 0; I
Another
#include
#include#include
#include
#include
using namespace std;int a[11];int mp[11][11];int vis[11];int n, kind;int result[400000][10];void init(){mp[1][3] = 2; mp[3][1] = 2;mp[1][7] = 4; mp[7][1] = 4;mp[1][9] = 5; mp[9][1] = 5;mp[2][8] = 5; mp[8][2] = 5;mp[3][7] = 5; mp[7][3] = 5;mp[3][9] = 6; mp[9][3] = 6;mp[4][6] = 5; mp[6][4] = 5;mp[7][9] = 8; mp[9][7] = 8;}bool can(){int v;vis[a[0]] = 1;for (int i = 1; i < n; i++){v = mp[a[i]][a[i - 1]];if (!(!vis[a[i]] && (!v || vis[v])))return false;vis[a[i]] = 1;}return true;}int main(){int t;cin >> t;init();while (t--){scanf("%d", &n);for (int i = 0; i