Given an Android 3x3 key lock screen and two integers m and n, where  ≤ m ≤ n ≤ , 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:
| | | |
| | | |
| | | |
Invalid move: - - -
Line - passes through key which had not been selected in the pattern. Invalid move: - - -
Line - passes through key which had not been selected in the pattern. Valid move: - - - -
Line - is valid because it passes through key , which had been selected in the pattern Valid move: - - - - -
Line - is valid because it passes through key , which had been selected in the pattern. Example:
Given m = , n = , return .

我自己的backtracking做法

最开始把cur设置为一个dummy value 0

 public class Solution {
int num = 0;
public int numberOfPatterns(int m, int n) {
for (int len=m; len<=n; len++) {
HashSet<Integer> visited = new HashSet<Integer>();
count(visited, 0, 0, len);
}
return num;
} public void count(HashSet<Integer> visited, int cur, int pos, int len) {
if (pos == len) {
num++;
return;
}
for (int elem=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;
}
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;
if (elem==9 && !visited.contains(6)) continue;
}
else if (cur == 4) {
if (elem == 6 && !visited.contains(5)) continue;
}
else if (cur == 6) {
if (elem == 4 && !visited.contains(5)) continue;
}
else if (cur == 7) {
if (elem==1 && !visited.contains(4)) continue;
if (elem==3 && !visited.contains(5)) continue;
if (elem==9 && !visited.contains(8)) continue;
}
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;
}
visited.add(elem);
count(visited, elem, pos+1, len);
visited.remove(elem);
}
}
}

最好的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.

 public class Solution {
// cur: the current position
// remain: the steps remaining
int DFS(boolean vis[], int[][] skip, int cur, int remain) {
if(remain < 0) return 0;
if(remain == 0) return 1;
vis[cur] = true;
int rst = 0;
for(int i = 1; i <= 9; ++i) {
// If vis[i] is not visited and (two numbers are adjacent or skip number is already visited)
if(!vis[i] && (skip[cur][i] == 0 || (vis[skip[cur][i]]))) {
rst += DFS(vis, skip, i, remain - 1);
}
}
vis[cur] = false;
return rst;
} public int numberOfPatterns(int m, int n) {
// Skip array represents number to skip between two pairs
int skip[][] = new int[10][10];
skip[1][3] = skip[3][1] = 2;
skip[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;
boolean vis[] = new boolean[10];
int rst = 0;
// DFS search each length from m to n
for(int i = m; i <= n; ++i) {
rst += 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); //
}
return rst;
}
}

Leetcode: Android Unlock Patterns的更多相关文章

  1. [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 ...

  2. [LeetCode] 351. Android Unlock Patterns 安卓解锁模式

    Given an Android 3x3 key lock screen and two integers m and n, where 1 ≤ m ≤ n ≤ 9, count the total ...

  3. [Swift]LeetCode351. 安卓解锁模式 $ Android Unlock Patterns

    Given an Android 3x3 key lock screen and two integers m and n, where 1 ≤ m ≤ n ≤ 9, count the total ...

  4. Android Unlock Patterns

    Given an Android 3x3 key lock screen and two integers m and n, where 1 ≤ m ≤ n ≤ 9, count the total ...

  5. LC 351. Android Unlock Patterns

    Given an Android 3x3 key lock screen and two integers m and n, where 1 ≤ m ≤ n ≤ 9, count the total ...

  6. 351. Android Unlock Patterns

    这个题我真是做得想打人了卧槽. 题目不难,就是算组合,但是因为是3乘3的键盘,所以只需要从1和2分别开始DFS,结果乘以4,再加上5开始的DFS就行了. 问题是这个傻逼题目的设定是,从1到8不需要经过 ...

  7. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  8. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  9. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

随机推荐

  1. Java整型与字符串相互转换

    >>>>>>>>>>>>>>>>>>>> 1如何将字串 String 转换成整数 ...

  2. sublime-生成html1.0

    文件--新建--输入--html:xt--CTRL+E.生成html1.0

  3. Codeforces Round #370 - #379 (Div. 2)

    题意: 思路: Codeforces Round #370(Solved: 4 out of 5) A - Memory and Crow 题意:有一个序列,然后对每一个进行ai = bi - bi  ...

  4. CodeChef - QCHEF 分块

    题目链接:http://vjudge.net/problem/174774/origin 题意:给定一个长度为n的序列a[],序列的值不大于m,现在有k个询问,每个询问给定(l,r).让你求出max{ ...

  5. 《DSP using MATLAB》示例Example5.9

    代码: n = 0:10; x = 10*(0.8) .^ n; y = x(mod_1(-n,11)+1); %% ----------------------------------------- ...

  6. cdn

    cdn内容分发网络: 1. 内容缓存Web cache技术,反向代理 2. 集群服务与负载均衡技术 LVS(四层) 与 nginx(七层) 3. 全局负载均衡工作原理:基于DNS解析的GSLB实现机制 ...

  7. HDU-1203(01背包)

    I NEED A OFFER! Problem Description Speakless 很早就想出国,现在他已经考完了所有需要的考试,准备了所有要准备的材料,于是,便需要去申请学校了.要申请国外的 ...

  8. jQuery 一些神奇的选择器写法

    ======================================================================== =========================== ...

  9. Unity Lightmap动态加载研究

    什么情况下需要Lightmap? 移动平台上目前暂时还不能开实时光影效果,会卡成幻灯片.所以就需要将光影烘焙到贴图上. 什么情况下需要动态加载Lightmap? 1.当项目抛弃了Unity的多场景模式 ...

  10. 在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求

    通用辅助类  下面是我编写的一个辅助类,在这个类中采用了HttpWebRequest中发送GET/HTTP/HTTPS请求,因为有的时候需 要获取认证信息(如Cookie),所以返回的是HttpWeb ...