[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 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:
| 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.
在安卓的3*3的解锁屏幕上,给出2个整数m, n(1 ≤ m ≤ n ≤ 9),问在m到n的滑动次数之间,有多少种可能的解锁方案。给出了合理和不合理的滑动。
优化方法是,由于 1,3,7,9 是对称的,2,4,6,8也是对称的,所以只用计算其中一个,然后乘以4,5是单独的一个,所以总共求3组就可以了。
解法:DFS,建立一个二维数组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调用一次,然后把所有的加起来就是最终结果。参考
Java:
public class Solution {
private int patterns;
private boolean valid(boolean[] keypad, int from, int to) {
if (from==to) return false;
int i=Math.min(from, to), j=Math.max(from,to);
if ((i==1 && j==9) || (i==3 && j==7)) return keypad[5] && !keypad[to];
if ((i==1 || i==4 || i==7) && i+2==j) return keypad[i+1] && !keypad[to];
if (i<=3 && i+6==j) return keypad[i+3] && !keypad[to];
return !keypad[to];
}
private void find(boolean[] keypad, int from, int step, int m, int n) {
if (step == n) {
patterns ++;
return;
}
if (step >= m) patterns ++;
for(int i=1; i<=9; i++) {
if (valid(keypad, from, i)) {
keypad[i] = true;
find(keypad, i, step+1, m, n);
keypad[i] = false;
}
}
}
public int numberOfPatterns(int m, int n) {
boolean[] keypad = new boolean[10];
for(int i=1; i<=9; i++) {
keypad[i] = true;
find(keypad, i, 1, m, n);
keypad[i] = false;
}
return patterns;
}
}
Java:
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); // 5
}
return rst;
}
}
Python:
# Time: O(9!)
# Space: O(9)
# Backtracking solution. (TLE)
class Solution_TLE(object):
def numberOfPatterns(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
def merge(used, i):
return used | (1 << i) def contain(used, i):
return bool(used & (1 << i)) def convert(i, j):
return 3 * i + j def numberOfPatternsHelper(m, n, level, used, i):
number = 0
if level > n:
return number if m <= level <= n:
number += 1 x1, y1 = divmod(i, 3)
for j in xrange(9):
if contain(used, j):
continue x2, y2 = divmod(j, 3)
if ((x1 == x2 and abs(y1 - y2) == 2) or
(y1 == y2 and abs(x1 - x2) == 2) or
(abs(x1 - x2) == 2 and abs(y1 - y2) == 2)) and \
not contain(used,
convert((x1 + x2) // 2, (y1 + y2) // 2)):
continue number += numberOfPatternsHelper(m, n, level + 1, merge(used, j), j) return number number = 0
# 1, 3, 7, 9
number += 4 * numberOfPatternsHelper(m, n, 1, merge(0, 0), 0)
# 2, 4, 6, 8
number += 4 * numberOfPatternsHelper(m, n, 1, merge(0, 1), 1)
# 5
number += numberOfPatternsHelper(m, n, 1, merge(0, 4), 4)
return number
Python:
# Time: O(9^2 * 2^9)
# Space: O(9 * 2^9)
# DP solution.
class Solution2(object):
def numberOfPatterns(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
def merge(used, i):
return used | (1 << i) def number_of_keys(i):
number = 0
while i > 0:
i &= i - 1
number += 1
return number def exclude(used, i):
return used & ~(1 << i) def contain(used, i):
return bool(used & (1 << i)) def convert(i, j):
return 3 * i + j # dp[i][j]: i is the set of the numbers in binary representation,
# d[i][j] is the number of ways ending with the number j.
dp = [[0] * 9 for _ in xrange(1 << 9)]
for i in xrange(9):
dp[merge(0, i)][i] = 1 res = 0
for used in xrange(len(dp)):
number = number_of_keys(used)
if number > n:
continue for i in xrange(9):
if not contain(used, i):
continue x1, y1 = divmod(i, 3)
for j in xrange(9):
if i == j or not contain(used, j):
continue x2, y2 = divmod(j, 3)
if ((x1 == x2 and abs(y1 - y2) == 2) or
(y1 == y2 and abs(x1 - x2) == 2) or
(abs(x1 - x2) == 2 and abs(y1 - y2) == 2)) and \
not contain(used,
convert((x1 + x2) // 2, (y1 + y2) // 2)):
continue dp[used][i] += dp[exclude(used, i)][j] if m <= number <= n:
res += dp[used][i] return res
Python:
# DP solution.
class Solution(object):
def numberOfPatterns(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
def merge(used, i):
return used | (1 << i) def number_of_keys(i):
number = 0
while i > 0:
i &= i - 1
number += 1
return number def contain(used, i):
return bool(used & (1 << i)) def convert(i, j):
return 3 * i + j # dp[i][j]: i is the set of the numbers in binary representation,
# dp[i][j] is the number of ways ending with the number j.
dp = [[0] * 9 for _ in xrange(1 << 9)]
for i in xrange(9):
dp[merge(0, i)][i] = 1 res = 0
for used in xrange(len(dp)):
number = number_of_keys(used)
if number > n:
continue for i in xrange(9):
if not contain(used, i):
continue if m <= number <= n:
res += dp[used][i] x1, y1 = divmod(i, 3)
for j in xrange(9):
if contain(used, j):
continue x2, y2 = divmod(j, 3)
if ((x1 == x2 and abs(y1 - y2) == 2) or
(y1 == y2 and abs(x1 - x2) == 2) or
(abs(x1 - x2) == 2 and abs(y1 - y2) == 2)) and \
not contain(used,
convert((x1 + x2) // 2, (y1 + y2) // 2)):
continue dp[merge(used, j)][j] += dp[used][i] return res
C++:
// DP solution.
class Solution {
public:
int numberOfPatterns(int m, int n) {
// dp[i][j]: i is the set of the numbers in binary representation,
// dp[i][j] is the number of ways ending with the number j.
vector<vector<int>> dp(1 << 9 , vector<int>(9, 0));
for (int i = 0; i < 9; ++i) {
dp[merge(0, i)][i] = 1;
} int res = 0;
for (int used = 0; used < dp.size(); ++used) {
const auto number = number_of_keys(used);
if (number > n) {
continue;
}
for (int i = 0; i < 9; ++i) {
if (!contain(used, i)) {
continue;
}
if (m <= number && number <= n) {
res += dp[used][i];
} const auto x1 = i / 3;
const auto y1 = i % 3;
for (int j = 0; j < 9; ++j) {
if (contain(used, j)) {
continue;
}
const auto x2 = j / 3;
const auto y2 = j % 3;
if (((x1 == x2 && abs(y1 - y2) == 2) ||
(y1 == y2 && abs(x1 - x2) == 2) ||
(abs(x1 - x2) == 2 && abs(y1 - y2) == 2)) &&
!contain(used, convert((x1 + x2) / 2, (y1 + y2) / 2))) {
continue;
}
dp[merge(used, j)][j] += dp[used][i];
}
}
} return res;
} private:
inline int merge(int i, int j) {
return i | (1 << j);
} inline int number_of_keys(int i) {
int number = 0;
for (; i; i &= i - 1) {
++number;
}
return number;
} inline bool contain(int i, int j) {
return i & (1 << j);
} inline int convert(int i, int j) {
return 3 * i + j;
}
};
C++:
// Time: O(9^2 * 2^9)
// Space: O(9 * 2^9)
// DP solution.
class Solution2 {
public:
int numberOfPatterns(int m, int n) {
// dp[i][j]: i is the set of the numbers in binary representation,
// dp[i][j] is the number of ways ending with the number j.
vector<vector<int>> dp(1 << 9 , vector<int>(9, 0));
for (int i = 0; i < 9; ++i) {
dp[merge(0, i)][i] = 1;
} int res = 0;
for (int used = 0; used < dp.size(); ++used) {
const auto number = number_of_keys(used);
if (number > n) {
continue;
}
for (int i = 0; i < 9; ++i) {
if (!contain(used, i)) {
continue;
} const auto x1 = i / 3;
const auto y1 = i % 3;
for (int j = 0; j < 9; ++j) {
if (i == j || !contain(used, j)) {
continue;
}
const auto x2 = j / 3;
const auto y2 = j % 3;
if (((x1 == x2 && abs(y1 - y2) == 2) ||
(y1 == y2 && abs(x1 - x2) == 2) ||
(abs(x1 - x2) == 2 && abs(y1 - y2) == 2)) &&
!contain(used, convert((x1 + x2) / 2, (y1 + y2) / 2))) {
continue;
}
dp[used][i] += dp[exclude(used, i)][j];
}
if (m <= number && number <= n) {
res += dp[used][i];
}
}
} return res;
} private:
inline int merge(int i, int j) {
return i | (1 << j);
} inline int number_of_keys(int i) {
int number = 0;
for (; i; i &= i - 1) {
++number;
}
return number;
} inline bool contain(int i, int j) {
return i & (1 << j);
} inline int exclude(int i, int j) {
return i & ~(1 << j);
} inline int convert(int i, int j) {
return 3 * i + j;
}
};
C++:
// Time: O(9!)
// Space: O(9)
// Backtracking solution.
class Solution3 {
public:
int numberOfPatterns(int m, int n) {
int number = 0;
// 1, 3, 5, 7
number += 4 * numberOfPatternsHelper(m, n, 1, merge(0, 0), 0);
// 2, 4, 6, 8
number += 4 * numberOfPatternsHelper(m, n, 1, merge(0, 1), 1);
// 5
number += numberOfPatternsHelper(m, n, 1, merge(0, 4), 4);
return number;
} private:
int numberOfPatternsHelper(int m, int n, int level, int used, int i) {
int number = 0;
if (level > n) {
return number;
}
if (level >= m) {
++number;
} const auto x1 = i / 3;
const auto y1 = i % 3;
for (int j = 0; j < 9; ++j) {
if (contain(used, j)) {
continue;
}
const auto x2 = j / 3;
const auto y2 = j % 3;
if (((x1 == x2 && abs(y1 - y2) == 2) ||
(y1 == y2 && abs(x1 - x2) == 2) ||
(abs(x1 - x2) == 2 && abs(y1 - y2) == 2)) &&
!contain(used, convert((x1 + x2) / 2, (y1 + y2) / 2))) {
continue;
}
number += numberOfPatternsHelper(m, n, level + 1, merge(used, j), j);
} return number;
} private:
inline int merge(int i, int j) {
return i | (1 << j);
} inline bool contain(int i, int j) {
return i & (1 << j);
} inline int convert(int i, int j) {
return 3 * i + j;
}
};
C++:
class Solution {
public:
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;
for(int i = 1; i<= 9; i++)
if(!visited[i] && visited[hash[num][i]])
cnt += DFS(m, n, len, i);
visited[num] = false;
return cnt;
}
int numberOfPatterns(int m, int n) {
if(m < 1 || n < 1) return 0;
visited.resize(10, false);
visited[0] = true;
hash.resize(10, vector<int>(10, 0));
hash[1][3] = hash[3][1] = 2;
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;
return DFS(m, n, 1, 1)*4 + DFS(m, n, 1, 2)*4 + DFS(m, n, 1, 5);
}
private:
vector<bool> visited;
vector<vector<int>> hash;
};
C++:
class Solution {
public:
int numberOfPatterns(int m, int n) {
return count(m, n, 0, 1, 1);
}
int count(int m, int n, int used, int i1, int j1) {
if (n == 0) return 1;
int res = (m <= 0);
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
// used2 check middle point has been used
int I = i1+i, J = j1+j, used2 = used | (1 << (i*3+j));
// used2 > used: add a new unused integer
// I%2 == 1: i1 odd i even or reverse
// used2 & (1 << I/2*3+J/2): mid point has been used
if (used2 > used && (I%2 || J%2 || used2 & (1 << I/2*3+J/2))) {
res += count(m-1, n-1, used2, i, j);
}
}
}
return res;
}
};
All LeetCode Questions List 题目汇总
[LeetCode] 351. Android Unlock Patterns 安卓解锁模式的更多相关文章
- [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 ...
- 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 ...
- 351. Android Unlock Patterns
这个题我真是做得想打人了卧槽. 题目不难,就是算组合,但是因为是3乘3的键盘,所以只需要从1和2分别开始DFS,结果乘以4,再加上5开始的DFS就行了. 问题是这个傻逼题目的设定是,从1到8不需要经过 ...
- [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 ...
- Leetcode: Android Unlock Patterns
Given an Android 3x3 key ≤ m ≤ n ≤ , count the total number of unlock patterns of the Android lock s ...
- Android Unlock Patterns
Given an Android 3x3 key lock screen and two integers m and n, where 1 ≤ m ≤ n ≤ 9, count the total ...
- 白底黑字!Android浅色状态栏黑色字体模式(另)
小彬什么都想做任重致远 关注 2016.06.30 10:16* 字数 489 阅读 3234评论 3喜欢 12 前言 由于该死不死的设计湿,设计了一套白色状态栏的UI.当然在iOS上可以实现自适应, ...
- Eclipse+ADT+Android SDK 搭建安卓开发环境
Eclipse+ADT+Android SDK 搭建安卓开发环境 要求 必备知识 windows 7 基本操作. 运行环境 windows 7(64位); eclipse-jee-luna-SR2 ...
- Android中的创建型模式总结
共5种,单例模式.工厂方法模式.抽象工厂模式.建造者模式.原型模式 单例模式 定义:确保某一个类的实例只有一个,而且向其他类提供这个实例. 单例模式的使用场景:某个类的创建需要消耗大量资源,new一个 ...
随机推荐
- Electrification Plan 最小生成树(prim+krusl+堆优化prim)
题目 题意: 无向图,给n个城市,n*n条边,每条边都有一个权值 代表修路的代价,其中有k个点有发电站,给出这k个点的编号,要每一个城市都连到发电站,问最小的修路代价. 思路: prim:把发电站之间 ...
- [USACO15DEC]最大流Max Flow(树上差分)
题目描述: Farmer John has installed a new system of N−1N-1N−1 pipes to transport milk between the NNN st ...
- P2602 [ZJOI2010]数字计数(递推)
P2602 [ZJOI2010]数字计数 思路: 首先考虑含有前导0的情况,可以发现在相同的\(i\)位数中,每个数的出现次数都是相等的.所以我们可以设\(f(i)\)为\(i\)位数每个数的出现次数 ...
- 阿里云——扩展Linux系统盘
前言 地址|https://help.aliyun.com/document_detail/111738.html?spm=a2c4g.11186623.2.7.1d284c07SFRBaq#sect ...
- modbus-poll和modbus-slave工具的学习使用——modbus协议功能码1的解析
一.数据解析 上一文介绍了modbus工具的基本使用情况,但是还没用说明modbus中的协议的具体意义, 1.左边是slave,id=1,说明地址是1,f=01说明是功能码01,功能码是一个字节,说明 ...
- 更丰富的符号工具包 Font Awesome
我时常想要在此类文档中通过一些图形符号来表达更丰富的含义或是对段落进行标注,例如使用 Emoji.然而 Emoji 在这方面仍然有存在一些不足,如: 颜色与文字风格不统一, 在不同系统的平台上显示不统 ...
- 洛谷 P3605 [USACO17JAN]Promotion Counting晋升者计数
题目描述 The cows have once again tried to form a startup company, failing to remember from past experie ...
- 洛谷 [USACO05DEC] 布局 题解
今天学了差分约束系统, 这是一道板子题. 核心:a[v]>a[u]+d 相当于从u到v连一条长度为d的有向边.由于要判断有环,所以要从0点先跑一遍spfa因为1点不一定能到所有的点. #incl ...
- BZOJ 5495: [2019省队联测]异或粽子 可持久化trie+堆
和超级钢琴,异或之三倍经验 $?$ 堆+贪心素质三连 $?$ 好无聊...... code: #include <bits/stdc++.h> #define N 500006 #defi ...
- 【洛谷P5158】 【模板】多项式快速插值
卡常严重,可有采用如下优化方案: 1.预处理单位根 2.少取几次模 3.复制数组时用 memcpy 4.进行多项式乘法项数少的时候直接暴力乘 5.进行多项式多点求值时如果项数小于500的话直接秦九昭展 ...