ACM学习历程—ZOJ 3861 Valid Pattern Lock(dfs)
2015-05-16 20:36
525 查看
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.
![](http://acm.zju.edu.cn/onlinejudge/showImage.do?name=valid_pattern_lock.png)
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 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
Sample Output
题目就是搜索。一共有8 + 8个方向。
需要注意的是:对于东南西北、东偏南等等的8个方向,如果被标记过,需要再往下搜索一步。
Map初始化需要全部置为-1,表示不可访问。
代码:
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.
![](http://acm.zju.edu.cn/onlinejudge/showImage.do?name=valid_pattern_lock.png)
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 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
题目就是搜索。一共有8 + 8个方向。
需要注意的是:对于东南西北、东偏南等等的8个方向,如果被标记过,需要再往下搜索一步。
Map初始化需要全部置为-1,表示不可访问。
代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #include <set> #include <map> #include <queue> #include <string> using namespace std; int xx[] = {-1, 1, -2, 2, -2, 2, -1, 1}; int yy[] = {-2, -2, -1, -1, 1, 1, 2, 2}; int Map[5][5], n, len; struct node { int val[9]; }p[370000]; bool cmp(node a, node b) { for (int i = 0; i < 9; ++i) if (a.val[i] != b.val[i]) return a.val[i] < b.val[i]; } void Dfs(int x, int y, int now) { p[len].val[now] = (x-1)*3+y; if (now == n - 1) { len++; p[len] = p[len-1]; return; } for (int xx = -1; xx <= 1; ++xx) { for (int yy = -1; yy <= 1; ++yy) { switch (Map[x+xx][y+yy]) { case -1: continue; case 0: Map[x+xx][y+yy] = 1; Dfs(x+xx, y+yy, now+1); Map[x+xx][y+yy] = 0; break; case 1: if (Map[x+xx*2][y+yy*2] == 0) { Map[x+xx*2][y+yy*2] = 1; Dfs(x+xx*2, y+yy*2, now+1); Map[x+xx*2][y+yy*2] = 0; } break; } } } for (int i = 0; i < 8; ++i) { if (x+xx[i] > 3 || x+xx[i] < 1) continue; if (y+yy[i] > 3 || y+yy[i] < 1) continue; if (Map[x+xx[i]][y+yy[i]] == 0) { Map[x+xx[i]][y+yy[i]] = 1; Dfs(x+xx[i], y+yy[i], now+1); Map[x+xx[i]][y+yy[i]] = 0; } } } void Work() { for (int i = 1; i < 4; ++i) for (int j = 1; j < 4; ++j) if (Map[i][j] == 0) { Map[i][j] = 1; Dfs(i, j, 0); Map[i][j] = 0; } sort(p, p+len, cmp); printf("%d\n", len); for (int i = 0; i < len; ++i) { for (int j = 0; j < n; ++j) { if (j) printf(" "); printf("%d", p[i].val[j]); } printf("\n"); } } void Input() { int v, x, y; len = 0; memset(Map, -1, sizeof(Map)); scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d", &v); x = (v-1)/3 + 1; y = v%3; if (!y) y = 3; Map[x][y] = 0; } /* for (int i = 0; i < 5; ++i) { for (int j = 0;j < 5; ++j) { printf("%3d", Map[i][j]); } printf("\n"); } */ } int main() { //freopen("test.in", "r", stdin); //freopen("test.out", "w", stdout); int T; scanf("%d", &T); for (int times = 0; times < T; ++times) { Input(); Work(); } return 0; }
相关文章推荐
- zoj 3861 Valid Pattern Lock (DFS)
- zoj 3861 Valid Pattern Lock(dfs)
- zoj 3861 Valid Pattern Lock(以及自己对dfs的一些理解)
- [水题+dfs] zoj 3861 Valid Pattern Lock
- ZOJ 3861 Valid Pattern Lock(深度优先遍历dfs,有限制条件的全排列)
- ZOJ 3861 Valid Pattern Lock DFS
- ZOJ - 3861 Valid Pattern Lock(dfs或其他,两种解法)
- DFS+模拟 ZOJ 3861 Valid Pattern Lock
- ZOJ 3861 Valid Pattern Lock
- 浙江大学2015年校赛B题 ZOJ 3861 Valid Pattern Lock
- zoj 3861 Valid Pattern Lock
- ZOJ 3861 - Valid Pattern Lock
- zoj3861——Valid Pattern Lock(全排列)
- ZOJ 3861 Valid Pattern Lock
- ZOJ 3861 Valid Pattern Lock
- Valid Pattern Lock-ZOJ 暴力DFS搜索
- ZOJ 3861 Valid Pattern Lock
- zoj 3861 Valid Pattern Lock(全排列 next_permutation 模拟)
- ZOJ 3861 Valid Pattern Lock
- ZOJ校赛——DFS+暴力——Valid Pattern Lock