晚饭时和同事聊到安卓屏幕解锁时会有多少种解锁方案,觉得很有趣,吃完饭开始想办法解题,花了大概2个小时解决。思路如下:

  1. 使用索引值0-9表示从左到右、从上到下的9个点,行、列号很容易从索引值得到;
  2. 使用一个列表(routeList)来表示解锁路径,列表的元素为点的索引值;
  3. 从任意点N出发(将index(N)放入routeList),判断与哪些点(集合NextNodes)可以构成合法路径;然后用递归的方式从NextNodes中的点出发,寻找下一个可以构成合法路径的点;直到再也无法找到可用的点供路径使用

下一步,就是在Nexus 7上捣鼓,发现Android定义合法的解锁路径必须满足下列条件:

  • 构成路径的点不得少于4个
  • 路径中不能有重复的点
  • (这点有点绕口)构成边或对角线的两点(例如[0,2]构成边,[0,8]构成对角线),不能在路径中相邻,除非其中点已经在路径中出现

接下来就是写代码了,Let's talk with the code!

问题的解是:

 ###############################################################
####### AndrUlkComb.py #########
## Calculate the COMBination count for ANDroid UNLock screen ##
## By grepall@gmail.com ##
############################################################### ###############################################################
## The Android unlock screen looks like below
## -------------------------------
## o -> o -> o
##
## o o o
##
## o o o
## --------------------------------
##
## We'll use:
## 1. Index from 0-8 to represent all 9 nodes instead of
## using row:column format.
## 2. An array (routeList in the code) to record the path of the unlock combination.
## For example, for the unlock comb which is shown in
## in the figure. There will be 3 elements in the array:
## 0, 1, and 2. (Although it's NOT a valid path because of
## not long enough)
##
## Valid unlock path must holds true for 3 conditions:
## C1: It must pass 4 different nodes at least;
## C2: It cannot contains duplicate nodes;
## C3: This is a little tricky. Any node cannot direct link to another node, when
## any node will be past through if we connect those 2 nodes in the unlock panel. UNLESS
## the crossed node already EXISTs in the path.
## For example, path (0, 2, 5, 4) is not valid, because 0-->2 crosses node 1. But path
## (1, 0, 2, 5) is a vliad path.
## This rule holds for diagonal as well. #######################################################################
# M stands for the width of the unlock panel
# H stands for the height of the unlock panel
# CAUTION: Modify this value will NOT generate correct result!!! That's TO BE DONE
#
W = 3 #Width
H = 3 #Height # Unlock path
routeList = list() # Combination count, which is we want to know
routeCount = 0 def isValidRoute(p1, route):
# If the candidate node exists in the unlock path
if route.count(p1) != 0:
return False
return True; def isCrossPoint(p1, p2):
# Will the connection line (p1, p2) cross another node?
x1 = p1%W
y1 = p1/W
x2 = p2%W
y2 = p2/W
if x1 == x2 and abs(y1-y2) > 1:
return True # same column
if y1 == y2 and abs(x1-x2) > 1:
return True # same row
if abs(y1-y2) == abs(x1-x2) and abs(y1-y2)>1:
return True # diagonal
return False def tryPoint(lastPt, route):
# Try next valid node for the unlock path recursively
global routeCount
for pt in range(0, W*H):
if isValidRoute(pt, route): #C2
crossPt = (lastPt+pt)/2
if isCrossPoint(lastPt, pt) and route.count(crossPt) == 0: #C3
continue
route.append(pt)
if len(route) >3: #C1
routeCount += 1
tryPoint(pt, route)
route.pop() # Each node may be the start node for unlock
for i in range(0, W*H):
routeList = [i]
tryPoint(i, routeList)
print "Start from point %d, combination count is %d now..." % (i, routeCount)
print "Toatl combination counter for Android unlock screen: " + str(routeCount)

计算Android屏幕解锁组合数的更多相关文章

  1. Android 修改屏幕解锁方式

    Android 修改屏幕解锁方式 问题 在手机第一次开机的时候,运行手机激活的APP 在激活APP允许过程中,当用户按电源键的时候,屏幕黑掉,进入锁屏状态 手机默认的锁屏是滑动解锁 用户这个时候再一次 ...

  2. Android Loader使用,屏幕解锁,重复荷载

    正在使用AsyncTaskLoader时间.当手机被解锁,重复加载数据,码,如以下: static class CouponShopQueryLoader extends AsyncTaskLoade ...

  3. Android监听屏幕解锁和判断屏幕状态

    开发后台服务的时候经常需要对屏幕状态进行判断,如果是想要监听屏幕解锁事件,可以在配置里面注册action为 android.intent.action.USER_PRESENT的广播,则可以监听解锁事 ...

  4. 【收藏】Android屏幕适配全攻略(最权威的Google官方适配指导)

    来源:http://blog.csdn.net/zhaokaiqiang1992 更多:Android AutoLayout全新的适配方式, 堪称适配终结者 Android的屏幕适配一直以来都在折磨着 ...

  5. Android屏幕适配全攻略(最权威的官方适配指导)(转),共大家分享。

    Android的屏幕适配一直以来都在折磨着我们这些开发者,本篇文章以Google的官方文档为基础,全面而深入的讲解了Android屏幕适配的原因.重要概念.解决方案及最佳实践,我相信如果你能认真的学习 ...

  6. Android屏幕适配全攻略(最权威的官方适配指导) (转)

    招聘信息: Cocos2d-X 前端主程 [新浪微博]手机客户端iOS研发工程师 20k-40k iOS 开发工程师 iOS高级开发工程师(中国排名第一的企业级移动互联网云计算公司 和创科技 红圈营销 ...

  7. Android 屏幕适配(一)百分比布局库(percent-support-lib) 解析与扩展

    转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/46695347: 本文出自:[张鸿洋的博客] 一.概述 周末游戏打得过猛,于是周 ...

  8. (转)android屏幕适配

    声明:eoe文章著作权属于作者,受法律保护,转载时请务必以超链接形式附带如下信息 原文作者: zhuangyujia 原文地址: http://my.eoe.cn/zhuangyujia/archiv ...

  9. 【转】Android屏幕适配全攻略(最权威的官方适配指导)

    原文网址:http://blog.csdn.net/jdsjlzx/article/details/45891551 Android的屏幕适配一直以来都在折磨着我们这些开发者,本篇文章以Google的 ...

随机推荐

  1. HDU 1285

    #include<stdio.h> #include<string.h> int degree[505],vis[505],map[501][501]; int main() ...

  2. [转]用C#实现的条形码和二维码编码解码器

    条形码的标准: 条形码的标准有ENA条形码.UPC条形码.二五条形码.交叉二五条形码.库德巴条形码.三九条形码和128条形码等,而商品上最常使用的就是EAN商品条形码.EAN商品条形码亦称通用商品条形 ...

  3. oracle 中的truncate 和delete

    一.查询表大小,块多少语句 Select SEGMENT_Name,BYTES,BLOCKS,Extents From dba_segments Where segment_name In('BAI_ ...

  4. FP树(附)

    Apriori算法和FPTree算法都是数据挖掘中的关联规则挖掘算法,处理的都是最简单的单层单维布尔关联规则. 转自http://blog.csdn.net/sealyao/article/detai ...

  5. 只需三步:使用C# 操作 Azure 队列

    Step 1 : 安装windows Azure package Step 2 : 配置文件增加: <appSettings> <add key="StorageConne ...

  6. HW4.39

    public class Solution { public static void main(String[] args) { double sum; double baseSalary = 500 ...

  7. HW1.5

    public class Solution { public static void main(String[] args) { System.out.println("(9.5 * 4.5 ...

  8. Find longest contiguous sub array

    It's still an Amazon interview question. Given an array containing only stars '*' and hashes '#' . F ...

  9. (二)在.net中如何使用Memcached

    Step1:第一步当然是下载Memcached for c# API,

  10. .NET托管代码和非托管代码

    .net托管代码是运行在.NET FRAMEWORK上的,类似于JAVA虚拟机托管代码:属安全代码,因为不涉及指针,但相对性能上较低,C#写出来的东西就可以认为是托管代码非托管代码:非安全的,可以使用 ...