We can treat the 3*3 grid as follows:-
4-7-9-6 is an invalid pattern lock because while joining 7-9 it passes through key 8 which has not been previously selected in pattern.
9-1-2-3 is an invalid pattern lock because while joining 9-1 it passes through key 5 which has not been previously selected in pattern.
4-2-6-8 is a valid pattern lock.
The first line contains a single integer ‘T’ representing the number of test cases.
The only line of each test case contains two space-separated integers ‘M’ and ‘N’ representing two integers for which you need to return the total number of valid patterns.
For each test case print an integer denoting the total number of valid mobile pattern locks which consist of at least ‘M’ keys and at most ‘N’ keys.
You do not need to print anything; it has already been taken care of. Just implement the function.
1 <= T <= 50
1 <= M <= N <= 9
Time Limit: 1sec
We can do DFS from all the starting points. We can easily observe 1,3,7, and 9 can be treated as the same i.e. the number of patterns for each one of them would be the same due to symmetry. Similarly, 2,4,6, and 8 will have the same number of patterns as a starting point. We can apply DFS as follows:
Capturing Grid
Rotting Oranges
Distance to a Cycle in Undirected Graph
Search In A Sorted 2D Matrix
Spiral Matrix