标签:acm算法 dfs algorithm iostream cstring
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<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<iostream> #include<algorithm> using namespace std; int n,a[15]; int ans[15]; bool ca[15]; bool used[15]; int cnt; bool judge(int cur,int last) { if(cur==1) { if(last==7 && (!ca[4] || !used[4]))return 0; if(last==3 && (!ca[2] || !used[2]))return 0; if(last==9 && (!ca[5] || !used[5]))return 0; } if(cur==2) { if(last==8 && (!ca[5] || !used[5]))return 0; } if(cur==3) { if(last==1 && (!ca[2] || !used[2]))return 0; if(last==7 && (!ca[5] || !used[5]))return 0; if(last==9 && (!ca[6] || !used[6]))return 0; } if(cur==4) { if(last==6 && (!ca[5] || !used[5]))return 0; } if(cur==6) { if(last==4 && (!ca[5] || !used[5]))return 0; } if(cur==7) { if(last==1 && (!ca[4] || !used[4]))return 0; if(last==3 && (!ca[5] || !used[5]))return 0; if(last==9 && (!ca[8] || !used[8]))return 0; } if(cur==8) { if(last==2 && (!ca[5] || !used[5]))return 0; } if(cur==9) { if(last==7 && (!ca[8] || !used[8]))return 0; if(last==3 && (!ca[6] || !used[6]))return 0; if(last==1 && (!ca[5] || !used[5]))return 0; } return 1; } void dfs_cnt(int dep,int last) { if(dep==n) { cnt++; } for(int i=1;i<=n;i++) { if(!used[a[i]] && judge(a[i],last)) { used[a[i]]=1; ans[dep]=a[i]; dfs_cnt(dep+1,a[i]); used[a[i]]=0; } } } void dfs_print(int dep,int last) { if(dep==n) { for(int i=0;i<n;i++) { if(!i)printf("%d",ans[i]); else printf(" %d",ans[i]); } printf("\n"); } for(int i=1;i<=n;i++) { if(!used[a[i]] && judge(a[i],last)) { used[a[i]]=1; ans[dep]=a[i]; dfs_print(dep+1,a[i]); used[a[i]]=0; } } } int main() { int T; scanf("%d",&T); while(T--) { scanf("%d",&n); for(int i=1;i<=9;i++) { used[i]=1; ca[i]=0; } for(int i=1;i<=n;i++) { scanf("%d",&a[i]); used[a[i]]=0; ca[a[i]]=1; } sort(a+1,a+n+1); cnt=0; dfs_cnt(0,0); printf("%d\n",cnt); dfs_print(0,0); } return 0; }
标签:acm算法 dfs algorithm iostream cstring
原文地址:http://blog.csdn.net/sky_miange/article/details/45014805