Android devices feature a unique security system: a 3x3 grid of dots that users connect to create unlock patterns. Imagine drawing lines between numbered dots (1-9) where each line segment connects two consecutive dots in your sequence.
The challenge? Not all connections are valid! Here are the rules:
- All dots must be unique - you can't use the same dot twice
- No jumping through unvisited dots - if your line passes through the center of another dot, that dot must have been visited earlier
For example:
1 2 3
4 5 6
7 8 9❌ Invalid: [4,1,3,6] - connecting 1→3 passes through dot 2, but 2 wasn't visited first
✅ Valid: [2,4,1,3,6] - now 1→3 is allowed because 2 was visited earlier
Your mission: Given integers m and n, count all unique valid unlock patterns with length between m and n dots (inclusive).
Input & Output
Visualization
Time & Space Complexity
Reduced by factor of 4 due to symmetry optimization, but still factorial in worst case
Recursion stack depth plus bitmask for visited state tracking
Constraints
- 1 ≤ m ≤ n ≤ 9
- Grid is always 3×3 with dots numbered 1-9
- Jump Rule: Cannot connect two dots if line passes through center of unvisited dot