LeetCode-Android Unlock Patterns

來源:互聯網
上載者:User

標籤:

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:

  1. Each pattern must connect at least m keys and at most n keys.
  2. All the keys must be distinct.
  3. 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.
  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 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 pattern

Valid 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.

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

Analysis:

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

Solution:

 1 public class Solution { 2     public int numberofPatternsRecur(boolean[] visited, int[][] skip, int cur, int curLen, int m, int n){ 3         if (curLen == n) return 1; 4          5         visited[cur] = true; 6         // If current pattern is longer than m, than count 1 pattern.  7         int totalNum = (curLen>=m) ? 1 : 0; 8         for (int i=1;i<=9;i++) 9             if (!visited[i] && visited[skip[cur][i]]){10                 totalNum += numberofPatternsRecur(visited,skip,i,curLen+1,m,n);11             }12         13         visited[cur] = false;14         return totalNum;15     }16     17     public int numberOfPatterns(int m, int n) {18         if (n<m || n<=0 || m<0 ) return 0;19         20         int[][] skip = new int[10][10];21         skip[1][3] = skip[3][1] = 2;22         skip[1][7] = skip[7][1] = 4;23         skip[3][9] = skip[9][3] = 6;24         skip[7][9] = skip[9][7] = 8;25         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;26         boolean[] visited = new boolean[10];27         // This is important, as all move that does not cross any number will query this.28         visited[0] = true;29         int res = 0;30         res += numberofPatternsRecur(visited,skip,1,1,m,n)*4;31         res += numberofPatternsRecur(visited,skip,2,1,m,n)*4;32         res += numberofPatternsRecur(visited,skip,5,1,m,n);33         return res;34     }35 }

 

LeetCode-Android Unlock Patterns

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.