G面经prepare: Android Phone Unlock Pattern

1 2 3
4 5 6
7 8 9
只有中间没有其他键的两个键才能相连,比如1可以连 2 4 5 6 8 但不能连 3 7 9
但是如果中间键被使用了,那就可以连,比如5已经被使用了,那1就可以连9
每个键只能用一次,给定一个长度L,求问有多少unique path with length L

Backtracking: 我的code不光可以知道数目,还可以打印所有Pattern

 1 package AndroidUnlockPattern;
 2 import java.util.*;
 3 
 4 public class Solution {
 5     int res = 0;
 6     HashSet<Integer> set = new HashSet<Integer>();
 7     static ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
 8     ArrayList<Integer> path = new ArrayList<Integer>();
 9     
10     public int calculate(int L) {
11         for (int i=1; i<=9; i++) {
12             set.add(i);
13         }
14         HashSet<Integer> visited = new HashSet<Integer>();
15         helper(0, 0, L, visited);
16         return res;
17     }
18     
19     public void helper(int cur, int pos, int L, HashSet<Integer> visited) {
20         if (pos == L) {
21             res++;
22             result.add(new ArrayList<Integer>(path));
23             return;
24         }
25         for (int elem : set) {
26             if (visited.contains(elem)) continue;
27             if (cur == 1) {
28                 if (elem==3 && !visited.contains(2)) continue;
29                 if (elem==7 && !visited.contains(4)) continue;
30                 if (elem==9 && !visited.contains(5)) continue;
31             }
32             else if (cur == 2) {
33                 if (elem==8 && !visited.contains(5)) continue;
34             }
35             else if (cur == 3) {
36                 if (elem==1 && !visited.contains(2)) continue;
37                 if (elem==7 && !visited.contains(5)) continue;
38                 if (elem==9 && !visited.contains(6)) continue;
39             }
40             else if (cur == 4) {
41                 if (elem == 6 && !visited.contains(5)) continue;
42             }
43             else if (cur == 6) {
44                 if (elem == 4 && !visited.contains(5)) continue;
45             }
46             else if (cur == 7) {
47                 if (elem==1 && !visited.contains(4)) continue;
48                 if (elem==3 && !visited.contains(5)) continue;
49                 if (elem==9 && !visited.contains(8)) continue;
50             }
51             else if (cur == 8) {
52                 if (elem==2 && !visited.contains(5)) continue;
53             }
54             else if (cur == 9) {
55                 if (elem==1 && !visited.contains(5)) continue;
56                 if (elem==3 && !visited.contains(6)) continue;
57                 if (elem==7 && !visited.contains(8)) continue;
58             }
59             visited.add(elem);
60             path.add(elem);
61             helper(elem, pos+1, L, visited);
62             visited.remove(elem);
63             path.remove(path.size()-1);
64         }
65     }
66     
67 
68     /**
69      * @param args
70      */
71     public static void main(String[] args) {
72         // TODO Auto-generated method stub
73         Solution sol = new Solution();
74         int res = sol.calculate(3);
75         System.out.println(res);
76         for (ArrayList<Integer> each : result) {
77             System.out.println(each);
78         }
79     }
80 
81 }
原文地址:https://www.cnblogs.com/EdwardLiu/p/5141771.html