[Leetcode] 351. Android Unlock Patterns 解题报告

来源:互联网 发布:新晨药业医药代表知乎 编辑:程序博客网 时间:2024/05/17 00:01

题目

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.

思路

用深度优先搜索+回溯是本题的正解。不过和最朴素的深搜与回溯相比,本题略微有些额外需要注意的地方:

1)在搜索过程中对路径的限制比较特殊,也就是说从A滑到B的时候,如果经过了C,那么C必须已经存在于当前搜索路径上了。这个问题我们用一张提前定义好的哈希表来解决,也就是根据键盘的布局,如果从A滑到B一定要经过C,那么就定义hash[A][B] = C。

2)只有在 m <= len <= n的时候,才产生计数。

3)可以通过合并同类项减少计算量,注意到1,3,7,9四个位置在安卓键盘上是处于对称位置,同样2,4,6,8也处于对称位置,所以在共有函数中,只需要分别从1,2开始深搜,然后将以结果乘以4即可。

代码

class Solution {public:    int numberOfPatterns(int m, int n) {        if(m < 1 || n < 1) {            return 0;        }        visited.resize(10, false);        visited[0] = true;                      // this makes the framework unified        hash.resize(10, vector<int>(10, 0));        hash[1][3] = hash[3][1] = 2;            // construct the passing numbers        hash[1][7] = hash[7][1] = 4;          hash[3][9] = hash[9][3] = 6;          hash[7][9] = hash[9][7] = 8;          hash[2][8] = hash[8][2] = hash[4][6] = hash[6][4] = 5;          hash[1][9] = hash[9][1] = hash[3][7] = hash[7][3] = 5;         // DFS(m, n, 1, 1) means start visiting from 1, 3, 7 or 9        // DFS(m, n, 1, 2) means start visiting from 2, 4, 6 or 8        // DFS(m, n, 1, 5) means start visiting from the center number 5        return DFS(m, n, 1, 1) * 4 + DFS(m, n, 1, 2) * 4 + DFS(m, n, 1, 5);    }private:    int DFS(int m, int n, int len, int num) {        int cnt = 0;          if(len >= m) {            cnt++;         }        if(len == n) {            return cnt;        }        visited[num] = true;    // try visiting num        for(int i = 1; i <= 9; i++) {            if(!visited[i] && visited[hash[num][i]] == true) {                cnt += DFS(m, n, len + 1, i);              }        }        visited[num] = false;   // backtracking        return cnt;      }    vector<bool> visited;    vector<vector<int>> hash;   // store the numbers between num1 and num2};

原创粉丝点击