Swipe to unlock is a common 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.
For the sake of description, we give these 9 points numbered 1-9 from top to bottom, left to right. Then 1->2->3 is not legal, because the length is not enough. 1->3->2->4 is also illegal, because 1->3 through the not yet passing point 2. 2->4->1->3->6 is legal, because the 1->3 point 2 has been crossed.
Inadvertently see this problem and in the answer to the various languages ah so I am bored this morning also want to do haha
I did not look at others code to see the correct results ~ because the feeling of searching search to find out good ah, according to their own thinking to write
I know the problem. Address: https://www.zhihu.com/question/24905007
Use DFS (deep search) to traverse all possible scenarios and filter out the impossible
First, use an array to list the things that can't happen.
filter[1][3]=filter[3][1]=2;filter[4][6]=filter[6][4]=5;filter[7][9]=filter[9][7]=8;filter[1][7]=filter[7][1]= 4;filter[2][8]=filter[8][2]=5;filter[3][9]=filter[9][3]=6;filter[1][9]=filter[9][1]=5;filter[3][7]=filter[7][3 ]=5;
And then the deep search is also screened.
void Dfs (int count) {if (count>=4) {result++;} for (int i=1;i<=9;i++) {if (Count>0&&!vis[filter[stamp[count-1]][i]) continue;if (!vis[i]) {vis[i]=1; Stamp[count]=i;dfs (count+1); vis[i]=0;}} return;}
All code:
#include <stdio.h> #include <string.h>int filter[10][10];int stamp[9];bool vis[10];int result;void dfs (int Count) {if (count>=4) {result++;} for (int i=1;i<=9;i++) {if (Count>0&&!vis[filter[stamp[count-1]][i]) continue;if (!vis[i]) {vis[i]=1; Stamp[count]=i;dfs (count+1); vis[i]=0;}} return;} int main () {memset (filter,0,sizeof (filter)); Filter[1][3]=filter[3][1]=2;filter[4][6]=filter[6][4]=5;filter[7][9] =filter[9][7]=8;filter[1][7]=filter[7][1]=4;filter[2][8]=filter[8][2]=5;filter[3][9]=filter[9][3]=6;filter[1][ 9]=filter[9][1]=5;filter[3][7]=filter[7][3]=5;result=0;vis[0]=true;dfs (0);p rintf ("%d\n", result);}
Results:
How many patterns can a phone's nine-gongge pattern be unlocked altogether? (Hiho analog face test 2-google online technical written test simulation)