Leetcode:android Unlock Patterns

Source: Internet
Author: User

Given an Android 3x3 keyLockscreen and integers m and N,where 1≤m≤n≤9, count the total number of unlock patterns of the AndroidLockScreen , which consist of minimum of M keys and maximum n keys. Rules fora valid Pattern:each pattern must connect at least m keys and at the most n keys. All the keys must is distinct. If the line connecting the consecutive keysinchThe pattern passes through any other keys, and the other keys must has previously selectedinchThe pattern. No jumps through non selected key isallowed. The order of keys used matters. Explanation:|1|2|3||4|5|6||7|8|9|Invalid Move:4-1-3-6 Line1-3Passes through key2Which had not been selectedinchThe pattern. Invalid Move:4-1-9-2 Line1-9Passes through key5Which had not been selectedinchThe pattern. Valid Move:2-4-1-3-6 Line1-3  isValid because it passes through key2, which had been selectedinchThe patternvalid move:6-5-4-1-9-2 Line1-9  isValid because it passes through key5, which had been selectedinchThe pattern. Example:given m=1, n =1,return 9.

My own backtracking practice.

Initially set cur to a dummy value 0

1  Public classSolution {2     intnum = 0;3      Public intNumberofpatterns (intMintN) {4          for(intLen=m; len<=n; len++) {5Hashset<integer> visited =NewHashset<integer>();6Count (visited, 0, 0, Len);7         }8         returnnum;9     }Ten      One      Public voidCount (Hashset<integer> visited,intCurintPosintLen) { A         if(pos = =Len) { -num++; -             return; the         } -          for(intElem=1; elem<=9; elem++) { -             if(Visited.contains (Elem))Continue; -             if(cur = = 1) { +                 if(Elem==3 &&!visited.contains (2))Continue; -                 if(Elem==7 &&!visited.contains (4))Continue; +                 if(Elem==9 &&!visited.contains (5))Continue; A             } at             Else if(cur = = 2) { -                 if(Elem = = 8 &&!visited.contains (5))Continue; -             } -             Else if(cur = = 3) { -                 if(Elem==1 &&!visited.contains (2))Continue; -                 if(Elem==7 &&!visited.contains (5))Continue; in                 if(Elem==9 &&!visited.contains (6))Continue; -             } to             Else if(cur = = 4) { +                 if(Elem = = 6 &&!visited.contains (5))Continue; -             } the             Else if(cur = = 6) { *                 if(Elem = = 4 &&!visited.contains (5))Continue; $             }Panax Notoginseng             Else if(cur = = 7) { -                 if(Elem==1 &&!visited.contains (4))Continue; the                 if(Elem==3 &&!visited.contains (5))Continue; +                 if(Elem==9 &&!visited.contains (8))Continue; A             } the             Else if(cur = = 8) { +                 if(elem==2 &&!visited.contains (5))Continue; -             } $             Else if(cur = = 9) { $                 if(Elem==1 &&!visited.contains (5))Continue; -                 if(Elem==3 &&!visited.contains (6))Continue; -                 if(Elem==7 &&!visited.contains (8))Continue; the             } - Visited.add (elem);WuyiCount (visited, Elem, pos+1, Len); the Visited.remove (elem); -         } Wu     } -}

Best Dfs with optimization beat 97%: https://discuss.leetcode.com/topic/46260/ Java-dfs-solution-with-clear-explanations-and-optimization-beats-97-61-12ms

Use the A matrix to store the corssed number for each possible move and use DFS to find out all patterns.

The optimization idea was that 1,3,7,9 was symmetric, 2,4,6,8 is also symmetric. Hence we only calculate one among each group and multiply by 4.

1  Public classSolution {2     //cur:the Current Position3     //remain:the Steps remaining4     intDFS (BooleanVis[],int[] Skip,intCurintremain) {5         if(Remain < 0)return0;6         if(remain = = 0)return1;7Vis[cur] =true;8         intRST = 0;9          for(inti = 1; I <= 9; ++i) {Ten             //If Vis[i] is not visited and (both numbers is adjacent or skip number is already visited) One             if(!vis[i] && (skip[cur][i] = = 0 | |(Vis[skip[cur][i]))) { ARST + = DFS (Vis, skip, I, remain-1); -             } -         } theVis[cur] =false; -         returnrst; -     } -      +      Public intNumberofpatterns (intMintN) { -         //Skip Array represents number to Skip between-pairs +         intSkip[][] =New int[10] [10]; ASKIP[1][3] = skip[3][1] = 2; atSKIP[1][7] = skip[7][1] = 4; -SKIP[3][9] = skip[9][3] = 6; -SKIP[7][9] = skip[9][7] = 8; -SKIP[1][9] = skip[9][1] = skip[2][8] = skip[8][2] = skip[3][7] = skip[7][3] = skip[4][6] = skip[6][4] = 5; -         BooleanVis[] =New Boolean[10]; -         intRST = 0; in         //DFS Search Each length from M to n -          for(inti = m; I <= N; ++i) { toRST + = DFS (Vis, skip, 1, i-1) * 4;//1, 3, 7, 9 are symmetric +RST + = DFS (Vis, skip, 2, i-1) * 4;//2, 4, 6, 8 are symmetric -RST + = DFS (Vis, Skip, 5, i-1);//5 the         } *         returnrst; $     }Panax Notoginseng}

Leetcode:android Unlock Patterns

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.