Android Unlock Patterns

时间:2022-09-24 12:01:28

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.

Android Unlock Patterns

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.

分析: http://www.cnblogs.com/grandyang/p/5541012.html

这道题乍一看题目这么长以为是一个设计题,其实不是,这道题还是比较有意思的,起码跟实际结合的比较紧密。这道题说的是安卓机子的解锁方法,有9个数字键,如果密码的长度范围在[m, n]之间,问所有的解锁模式共有多少种,注意题目中给出的一些非法的滑动模式。那么我们先来看一下哪些是非法的,首先1不能直接到3,必须经过2,同理的有4到6,7到9,1到7,2到8,3到9,还有就是对角线必须经过5,例如1到9,3到7等。我们建立一个二维数组jumps,用来记录两个数字键之间是否有中间键,然后再用一个一位数组visited来记录某个键是否被访问过,然后我们用递归来解,我们先对1调用递归函数,在递归函数中,我们遍历1到9每个数字next,然后找他们之间是否有jump数字,如果next没被访问过,并且jump为0,或者jump被访问过,我们对next调用递归函数。数字1的模式个数算出来后,由于1,3,7,9是对称的,所以我们乘4即可,然后再对数字2调用递归函数,2,4,6,9也是对称的,再乘4,最后单独对5调用一次,然后把所有的加起来就是最终结果了,参见代码如下:

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

Reference:

http://massivealgorithms.blogspot.com/2016/06/leetcode-351-android-unlock-patterns.html

Android Unlock Patterns的更多相关文章

  1. &lbrack;LeetCode&rsqb; 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&colon; Android Unlock Patterns

    Given an Android 3x3 key ≤ m ≤ n ≤ , count the total number of unlock patterns of the Android lock s ...

  3. &lbrack;Swift&rsqb;LeetCode351&period; 安卓解锁模式 &dollar; 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. &lbrack;LeetCode&rsqb; 351&period; 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&period; 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&period; Android Unlock Patterns

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

  7. LeetCode All in One 题目讲解汇总&lpar;持续更新中&period;&period;&period;&rpar;

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

  8. Swift LeetCode 目录 &vert; Catalog

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

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

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

随机推荐

  1. Leetcode 377&period; Combination Sum IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  2. ios开发应用内实现多语言*切换

    需求描述:应用内部有一按钮,点击切换语言(如中英文切换).说起来这个是好久以前做的一个功能点了,刚开始也是没有头绪,后来解决了发现很简单,把方法分享一下.1.原理.查看NSLocalizedStrin ...

  3. 如何在虚拟机中安装Win7系统

    在虚拟机里安装系统,可以很方便我们对系统的各项功能进行测试,而又不会影响本机系统,本节就介绍如何在虚拟机中安装Win7系统 . 工具/原料 vm9虚拟机 电脑一台 方法/步骤 1 在百度上搜索win7 ...

  4. PHP于Post和Get得到的数据写入到文件中

    有时Post要么Get越过那我们不知道什么样的形状数据,它可以是JSON格风格或只是简单地通过数据.这一次,我们能够把他写的文字,传过来的数据是什么格式了. $val = ""; ...

  5. 【1】JavaScript编程全解笔记(一)

    1.概述 本书涵盖了 JavaScript 各个方面的主题,从客户端以及服务端 JavaScript 等基础内容,主要讲了  HTML5.Web API.Node.js 与 WebSocket 等技术 ...

  6. leetcode--012 single number I

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA0MAAADGCAIAAACfN8xOAAAZ6UlEQVR4nO3dsZKcurbG8fNOnd1nIv

  7. java 里面保留字volatile及其与synchronized的区别

           锁提供了两种主要特性:互斥(mutual exclusion) 和可见性(visibility).互斥即一次只允许一个线程持有某个特定的锁,因此可使用该特性实现对共享数据的协调访问协议, ...

  8. RTMPdump(libRTMP) 源代码分析 8: 发送消息(Message)

    ===================================================== RTMPdump(libRTMP) 源代码分析系列文章: RTMPdump 源代码分析 1: ...

  9. HDU 4818 Golden Radio Base (2013长春现场赛B题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4814 进制转换. 现场根据题目给的两个公式,不断更新!!! 胡搞就可以了. 现场3A,我艹,一次循环开 ...

  10. Winform判断EventHandler是否已经添加

    斜体部分替换成自己需要的 private bool HasValueChangedEventHandler(DateTimePicker b) { FieldInfo f1 = typeof(Date ...