这道题目是队友写的,貌似是用暴力枚举出来。
题意:给出一组数,要求这组数在解锁的界面可能的滑动序列。
思路:按照是否能够直接到达建图,如1可以直接到2,但是1不能直接到3,因为中间必须经过一个2。
要注意的假如2已结访问过,那么1就可以直接到2。
建图DFS,图要更新。
Source Code:
#include <stdio.h> #include <string.h> int node[10], ans[10], n, vis[10], k, fun[1000000][10]; int map[10][10] = { {0}, {0,99,0,2,0,0,0,4,0,5}, {0,0,99,0,0,0,0,0,5,0}, {0,2,0,99,0,0,0,5,0,6}, {0,0,0,0,99,0,5,0,0,0}, {0,0,0,0,0,99,0,0,0,0}, {0,0,0,0,5,0,99,0,0,0}, {0,4,0,5,0,0,0,99,0,8}, {0,0,5,0,0,0,0,0,99,0}, {0,5,0,6,0,0,0,8,0,99} }; void dfs( int now, int count ){ int i; if( count == n ){ for( i=0; i<n; i++ ) fun[k][i] = ans[i]; k++; return ; } for( i=1; i<10; i++ ){ if( map[now][i]!=99 && node[i] ){ if( !map[now][i] && !vis[i] ){ vis[i] = 1; ans[count] = i; dfs(i,count+1); vis[i] = 0; } else if( map[now][i] && !vis[i] && vis[map[now][i]] ){ vis[i] = 1; ans[count] = i; dfs(i,count+1); vis[i] = 0; } } } } int main(){ int t, temp, i, j; scanf("%d",&t); while( t-- ){ k = 0; memset(node,0,sizeof(node)); scanf("%d",&n); for( i=0; i<n; i++ ){ scanf("%d",&temp); node[temp] = 1; } for( i=1; i<10; i++ ){ if( node[i] ){ memset(ans,0,sizeof(ans)); memset(vis,0,sizeof(vis)); ans[0] = i; vis[i] = 1; dfs(i,1); } } printf("%d\n", k ); for( i=0; i<k; i++ ){ for( j=0; j<n-1; j++ ) printf("%d ",fun[i][j]); printf("%d\n",fun[i][j]); } } return 0; }