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. linux学习笔记-前篇

    大学毕业已经快三年了,从事嵌入式开发的工作也快三年了. 不过,老干些裸机开发,感觉很是枯燥,一咬牙一跺脚,决定从今天开始学习Linux操作系统,顺便记录下学习过程中所遇到的问题与心得. 自己从前完全没 ...

  2. mysql like 贪婪匹配 同时匹配多个值

    LIKE "%a%b%c%",这样匹配出的就是包含a,b,c三个关键词的记录  (三个关键词不在一起时) 不好用 mysql> select count(1) from dm ...

  3. 常用的Meta标签写法和作用

    页面关键词 <meta name="keywords" content="your tags" /> 页面描述 <meta name=&quo ...

  4. ZeroMQ接口函数之 :zmq_setsockopt –设置ZMQ socket的属性

    ZeroMQ API 目录 :http://www.cnblogs.com/fengbohello/p/4230135.html 本文地址 :http://www.cnblogs.com/fengbo ...

  5. jQuery的封装方式与JS中new的实现原理

    function jQuery() { return new jQuery.fn.init(); } jQuery.fn = jQuery.prototype = { init: function() ...

  6. 【Scala】尾递归优化

    以递归方式思考 递归通过灵巧的函数定义,告诉计算机做什么.在函数式编程中,随处可见递归思想的运用.下面给出几个递归函数的例子: object RecursiveExample extends App{ ...

  7. CI 资源文件加入模板

    CI  资源文件加入模板: (资源文件:图片,css,js ,业务文件csv,txt.....) 1.引入url辅助函数库   helper 2.使用base_url函数  生成文件物理硬盘地址 3. ...

  8. PDMS二次开发之PML开发一些常见查询语句

    1.查找session 以及session number var !DBname DBname !db = object db(!DBname) !session = !db.lastsession( ...

  9. Xshell远程连接工具

    下载地址:http://rj.baidu.com/soft/detail/15201.html?ald Xshell 是一个强大的安全终端模拟软件,它支持SSH1, SSH2, 以及Microsoft ...

  10. maven 依赖

    依赖排除 当一个项目A依赖项目B,而项目B同时依赖项目C,如果项目A中因为各种原因不想引用项目C,在配置项目B的依赖时,可以排除对C的依赖. 示例(假设配置的是A的pom.xml,依赖关系为:A -- ...