Leetcode-android Unlock Patterns

Source: Internet
Author: User

Given an Android 3x3 key lock screens and integers m and n, where 1≤m≤n≤9, count the T Otal 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:

    1. Each of the pattern must connect at least m keys and at the most n keys.
    2. All the keys must is distinct.
    3. If the line connecting and consecutive keys in the pattern passes through any other keys, the other keys must has Previou Sly selected in the pattern. No jumps through non selected key is allowed.
    4. 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 isn't been selected in the pattern.

Invalid Move:4 - 1 - 9 - 2
Line 1-9 passes through Key 5 which had isn't been selected in the pattern.

Valid Move:2 - 4 - 1 - 3 - 6
Line 1-3 are valid because it passes through Key 2, which had been selected in the pattern

Valid Move:6 - 5 - 4 - 1 - 9 - 2
Line 1-9 are valid because it passes through key 5, which had been selected in the pattern.

Example:
Given m = 1, n = 1, return 9.

Credits:
Special thanks to @elmirap for adding this problem and creating all test cases.

Analysis:

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

Solution:

1  Public classSolution {2      Public intNumberofpatternsrecur (Boolean[] visited,int[] Skip,intCurintCurlen,intMintN) {3         if(Curlen = = N)return1;4         5Visited[cur] =true;6         //If current pattern is longer than M, than Count 1 pattern.7         intTotalnum = (curlen>=m)? 1:0;8          for(inti=1;i<=9;i++)9             if(!visited[i] &&Visited[skip[cur][i]]) {TenTotalnum + = Numberofpatternsrecur (visited,skip,i,curlen+1, m,n); One             } A          -Visited[cur] =false; -         returnTotalnum; the     } -      -      Public intNumberofpatterns (intMintN) { -         if(n<m | | n<=0 | | m<0)return0; +          -         int[] Skip =New int[10] [10]; +SKIP[1][3] = skip[3][1] = 2; ASKIP[1][7] = skip[7][1] = 4; atSKIP[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; -         Boolean[] visited =New Boolean[10]; -         //This is a important, as all move, which does not cross any number would query this. -Visited[0] =true; in         intres = 0; -Res + = Numberofpatternsrecur (visited,skip,1,1,m,n); toRes + = Numberofpatternsrecur (visited,skip,2,1,m,n); +Res + = Numberofpatternsrecur (visited,skip,5,1, m,n); -         returnRes; the     } *}

Leetcode-android Unlock Patterns

Related Article

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.