计算Android屏幕解锁组合数
晚饭时和同事聊到安卓屏幕解锁时会有多少种解锁方案,觉得很有趣,吃完饭开始想办法解题,花了大概2个小时解决。思路如下:
- 使用索引值0-9表示从左到右、从上到下的9个点,行、列号很容易从索引值得到;
- 使用一个列表(routeList)来表示解锁路径,列表的元素为点的索引值;
- 从任意点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屏幕解锁组合数的更多相关文章
- Android 修改屏幕解锁方式
Android 修改屏幕解锁方式 问题 在手机第一次开机的时候,运行手机激活的APP 在激活APP允许过程中,当用户按电源键的时候,屏幕黑掉,进入锁屏状态 手机默认的锁屏是滑动解锁 用户这个时候再一次 ...
- Android Loader使用,屏幕解锁,重复荷载
正在使用AsyncTaskLoader时间.当手机被解锁,重复加载数据,码,如以下: static class CouponShopQueryLoader extends AsyncTaskLoade ...
- Android监听屏幕解锁和判断屏幕状态
开发后台服务的时候经常需要对屏幕状态进行判断,如果是想要监听屏幕解锁事件,可以在配置里面注册action为 android.intent.action.USER_PRESENT的广播,则可以监听解锁事 ...
- 【收藏】Android屏幕适配全攻略(最权威的Google官方适配指导)
来源:http://blog.csdn.net/zhaokaiqiang1992 更多:Android AutoLayout全新的适配方式, 堪称适配终结者 Android的屏幕适配一直以来都在折磨着 ...
- Android屏幕适配全攻略(最权威的官方适配指导)(转),共大家分享。
Android的屏幕适配一直以来都在折磨着我们这些开发者,本篇文章以Google的官方文档为基础,全面而深入的讲解了Android屏幕适配的原因.重要概念.解决方案及最佳实践,我相信如果你能认真的学习 ...
- Android屏幕适配全攻略(最权威的官方适配指导) (转)
招聘信息: Cocos2d-X 前端主程 [新浪微博]手机客户端iOS研发工程师 20k-40k iOS 开发工程师 iOS高级开发工程师(中国排名第一的企业级移动互联网云计算公司 和创科技 红圈营销 ...
- Android 屏幕适配(一)百分比布局库(percent-support-lib) 解析与扩展
转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/46695347: 本文出自:[张鸿洋的博客] 一.概述 周末游戏打得过猛,于是周 ...
- (转)android屏幕适配
声明:eoe文章著作权属于作者,受法律保护,转载时请务必以超链接形式附带如下信息 原文作者: zhuangyujia 原文地址: http://my.eoe.cn/zhuangyujia/archiv ...
- 【转】Android屏幕适配全攻略(最权威的官方适配指导)
原文网址:http://blog.csdn.net/jdsjlzx/article/details/45891551 Android的屏幕适配一直以来都在折磨着我们这些开发者,本篇文章以Google的 ...
随机推荐
- lightoj 1004
很水的一个dp #include<cstdio> #include<iostream> #include<cstring> #include<algorith ...
- imdisk命令行使用及配置
imdisk是一个开源的虚拟磁盘软件,集虚拟光驱,文件虚拟光驱,映射物理磁盘,映射物理内存等功能 如果使用devio--Device I/O Service,可以映射网络磁盘等. 通用于windows ...
- OpenGL绘制自由落体小球
OpenGL绘制自由落体小球 一. 程序运行的软硬件环境 本次设计在window10系统下进行,运用C++进行编写,在CodeBlocks环境下使用OpenGL进行设计. 所需环境配置分为2部分 ...
- Windows下部署Git Server端
解决方案一: (注意:如果英文阅读没太大障碍,直接到Bonobo官网看相关文档即可.) win7(windows server 2008应该也可以,没测试过)+ IIS7.5(win7自带的)+ Gi ...
- ZOJ-2365 Strong Defence 贪心,BFS
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2365 我没看懂题目...这样理解:一个有向图,要给一些边染色,使 ...
- 取得MSSQL表中字段及主键等属性SQL语法
SELECT c.NAME AS [Column Name], t.NAME AS [Data type], c.max_length AS [Max Length], c.precision, c. ...
- PHP函数积累总结(Math函数、字符串函数、数组函数)
Math函数:10个较常用标红.abs — 绝对值acos — 反余弦acosh — 反双曲余弦asin — 反正弦asinh — 反双曲正弦atan2 — 两个参数的反正切atan — 反正切ata ...
- SPOJ4206Fast Maximum Matching(hopcroft-karp)
题目请戳这里 题目大意:裸的二分匹配. 题目分析:数据比较强,用来测模版的.这题用hungry跑着会比较吃力,所以用hopcroft-karp算法.这个算法较hungry高效是因为每次bfs找到一个增 ...
- vijos 1464 NOIP 1997 积木游戏
背景 1997年全国青少年信息学(计算机)奥林匹克竞赛试题 第二试 描述 积木游戏 SERCOI 最近设计了一种积木游戏.每个游戏者有N块编号依次为1 ,2,…,N的长方体积木.对于每块积木,它的三条 ...
- JBPM流程实例(PI)Process Instance
/** * 流程实例 * * 启动流程实例 * * 完成任务 * * 查询 * * 查询流程实例 * * 查询任务 * * 查询正在 ...