A mobile pattern is a grid of 3X3 cell, where drawing a specific pattern (connecting specific sequence of cells in order) will unlock the mobile. In this problem, the task is to calculate number of ways of making the lock pattern with number of connections in given range. In general terms, we are given a range as min and max, we need to tell how many patterns can be made which use at least min connection cell and at most max connection cell.
A simple solution is to do simple DFS by going through various connections from all starting points. We can optimize by seeing the symmetry between patterns, we can treat cell 1, 3, 7 and 9 as same. Similarly, we can treat 2, 4, 6 and 8 as same. Answer returned by a member of same group can be multiplied by 4 to get result by all members.
Next thing to note is that below patterns are not valid, because jumping some cell is not allowed as in below diagram cell 8 and cell 5, 6 are jumped.
To take care of such invalid moves, a 2D jump array is taken in below code which stores possible jump cells in jump array. When we call recursively we impose an extra condition that if we are moving from one cell to another which involves some jumping cell, then this cell should be visited already to ignore invalid moves.
In below code we have made recursive calls from 1, 2 and 5 only because 1 will cover 3, 5 and 7 and 2 will cover 4, 6 and 8 due to symmetry.
See below code for better understanding :
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Ways of selecting men and women from a group to make a team
- Ways to represent a number as a sum of 1's and 2's
- Number of ways to go from one point to another in a grid
- Number of ways to reach the end of matrix with non-zero AND value
- Number of ways to get even sum by choosing three numbers from 1 to N
- Number of ways to pair people
- Number of ways to cut a stick of length N into K pieces
- Count ways to express even number ‘n’ as sum of even integers
- Number of ways to merge two arrays such retaining order
- Number of ways to arrange a word such that no vowels occur together
- Number of ways to select a node from each connected component
- Number of ways to reach Nth floor by taking at-most K leaps
- Number of ways of scoring R runs in B balls with at most W wickets
- Number of ways to arrange a word such that all vowels occur together
- Number of ways to remove a sub-string from S such that all remaining characters are same