Mobile phone 9 The total number of pattern unlocking methods

Source: Internet
Author: User

pattern Unlock is a commonly used feature of smartphones. You need to move from one point to another on a 3x3 lattice, moving repeatedly to an "adjacent" point that has not yet passed. These crossed points are composed of a zigzag, and if the default line in the pattern, the direction of the same, then the phone will be unlocked. Two points are adjacent and only if there are no points that have not been passed on the segment with these two points as the endpoints . In addition, this polyline requires at least 4 points.

Some people may have the same question as me, in this small 9 points, the pattern password total how many forms of it? Today I'll use a depth-first search method to solve this problem. The specific C + + code is as follows:

#include <iostream>using namespace Std;int isvisit[10]={0};//Indicates whether the point is currently in the path int nopath[8][3]={{1,3,2},{4,6,5},{ 7,9,8},{1,7,4},{2,8,5},{3,9,6},{1,9,5},{3,7,5}};//three numbers for each item {A,B,C} means: When point C is not on the path, <span style= "font-family: ' Helvetica Neue ', Helvetica, Arial, Sans-serif; > Point A and point B cannot be directly connected to </span>int J,k;
int haspath (int start,int end)//Determine if the two points can be directly connected to {for (j=0;j<8;j++) {if (start==nopath[j][0]&&end==nopath[j ][1]) | | (Start==nopath[j][1]&&end==nopath[j][0])) {if (isvisit[nopath[j][2]]) <span style= "White-space:pre" ></span>//means that point C is already on the path, A and b directly can connect directly to return 1;else return 0;}} return 1;} int dfs (int start,int length) {int count = 0;isvisit[start]=1;int i;for (i=1;i<10;i++) <span style= "White-space:pre "></span>//iterates through all the points for access {if (I==start | | Isvisit[i]) continue;if (Haspath (start,i)) {count + = DFS (i,length+1); <span style= "White-space:pre" ></span >//Recursive search}}isvisit[start]=0;if (length>=4) <span style= "White-space:pre" ></span>//if the path length exceeds 4, The path meets the requirements Count++;return count;} int main () {int result=0;for (k=1;k<10;k++) <span style= "White-space:pre" ></span>//starting from 9 points, respectively, to increase the efficiency of the DFS, Here you can reduce unnecessary operations Result+=dfs (k,1) based on the symmetry principle, cout << result << endl;return 0;}

The result of the operation is: 389112


Mobile phone 9 The total number of pattern unlocking methods

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.