Leetcode: Android Unlock Patterns

人生必须的知识就是引人向光明方面的明灯。这篇文章主要讲述Leetcode: Android Unlock Patterns相关的知识,希望能为你提供帮助。

Given an android 3x3 key lock screen and two integers m and n, where 1 ≤ m ≤ n ≤ 9, count the total number of unlock patterns of the Android lock screen, which consist of minimum of m keys and maximum n keys.Rules for a valid pattern: Each pattern must connect at least m keys and at most n keys. All the keys must be distinct. If the line connecting two consecutive keys in the pattern passes through any other keys, the other keys must have previously selected in the pattern. No jumps through non selected key is allowed. The order of keys used matters.Explanation: | 1 | 2 | 3 | | 4 | 5 | 6 | | 7 | 8 | 9 | Invalid move: 4 - 1 - 3 - 6 Line 1 - 3 passes through key 2 which had not been selected in the pattern.Invalid move: 4 - 1 - 9 - 2 Line 1 - 9 passes through key 5 which had not been selected in the pattern.Valid move: 2 - 4 - 1 - 3 - 6 Line 1 - 3 is valid because it passes through key 2, which had been selected in the patternValid move: 6 - 5 - 4 - 1 - 9 - 2 Line 1 - 9 is valid because it passes through key 5, which had been selected in the pattern.Example: Given m = 1, n = 1, return 9.

我自己的backtracking做法
最开始把cur设置为一个dummy value 0 
1 public class Solution { 2int num = 0; 3public int numberOfPatterns(int m, int n) { 4for (int len=m; len< =n; len++) { 5HashSet< Integer> visited = new HashSet< Integer> (); 6count(visited, 0, 0, len); 7} 8return num; 9} 10 11public void count(HashSet< Integer> visited, int cur, int pos, int len) { 12if (pos == len) { 13num++; 14return; 15} 16for (int elem=1; elem< =9; elem++) { 17if (visited.contains(elem)) continue; 18if (cur == 1) { 19if (elem==3 & & !visited.contains(2)) continue; 20if (elem==7 & & !visited.contains(4)) continue; 21if (elem==9 & & !visited.contains(5)) continue; 22} 23else if (cur == 2) { 24if (elem == 8 & & !visited.contains(5)) continue; 25} 26else if (cur == 3) { 27if (elem==1 & & !visited.contains(2)) continue; 28if (elem==7 & & !visited.contains(5)) continue; 29if (elem==9 & & !visited.contains(6)) continue; 30} 31else if (cur == 4) { 32if (elem == 6 & & !visited.contains(5)) continue; 33} 34else if (cur == 6) { 35if (elem == 4 & & !visited.contains(5)) continue; 36} 37else if (cur == 7) { 38if (elem==1 & & !visited.contains(4)) continue; 39if (elem==3 & & !visited.contains(5)) continue; 40if (elem==9 & & !visited.contains(8)) continue; 41} 42else if (cur == 8) { 43if (elem==2 & & !visited.contains(5)) continue; 44} 45else if (cur == 9) { 46if (elem==1 & & !visited.contains(5)) continue; 47if (elem==3 & & !visited.contains(6)) continue; 48if (elem==7 & & !visited.contains(8)) continue; 49} 50visited.add(elem); 51count(visited, elem, pos+1, len); 52visited.remove(elem); 53} 54} 55 }

最好的DFS with Optimization beat 97%: https://discuss.leetcode.com/topic/46260/java-dfs-solution-with-clear-explanations-and-optimization-beats-97-61-12ms
Use an matrix to store the corssed number for each possible move and use DFS to find out all patterns.
The optimization idea is that 1,3,7,9 are symmetric, 2,4,6,8 are also symmetric. Hence we only calculate one among each group and multiply by 4.
1 public class Solution { 2// cur: the current position 3// remain: the steps remaining 4int DFS(boolean vis[], int[][] skip, int cur, int remain) { 5if(remain < 0) return 0; 6if(remain == 0) return 1; 7vis[cur] = true; 8int rst = 0; 9for(int i = 1; i < = 9; ++i) { 10// If vis[i] is not visited and (two numbers are adjacent or skip number is already visited) 11if(!vis[i] & & (skip[cur][i] == 0 || (vis[skip[cur][i]]))) { 12rst += DFS(vis, skip, i, remain - 1); 13} 14} 15vis[cur] = false; 16return rst; 17} 18 19public int numberOfPatterns(int m, int n) { 20// Skip array represents number to skip between two pairs 21int skip[][] = new int[10][10]; 22skip[1][3] = skip[3][1] = 2; 23skip[1][7] = skip[7][1] = 4; 24skip[3][9] = skip[9][3] = 6; 25skip[7][9] = skip[9][7] = 8; 26skip[1][9] = skip[9][1] = skip[2][8] = skip[8][2] = skip[3][7] = skip[7][3] = skip[4][6] = skip[6][4] = 5; 27boolean vis[] = new boolean[10]; 28int rst = 0; 29// DFS search each length from m to n 30for(int i = m; i < = n; ++i) { 31rst += DFS(vis, skip, 1, i - 1) * 4; // 1, 3, 7, 9 are symmetric 32rst += DFS(vis, skip, 2, i - 1) * 4; // 2, 4, 6, 8 are symmetric 33rst += DFS(vis, skip, 5, i - 1); // 5 34} 35return rst; 36} 37 }

【Leetcode: Android Unlock Patterns】 

    推荐阅读