Android Unlock Patterns - Problem
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
example_1.py — Basic Range
$
Input:
m = 1, n = 1
›
Output:
9
💡 Note:
All single-dot patterns are valid: [1], [2], [3], [4], [5], [6], [7], [8], [9]. Each dot can be a valid pattern of length 1.
example_2.py — Small Range
$
Input:
m = 1, n = 2
›
Output:
65
💡 Note:
Includes all 9 single-dot patterns plus all valid 2-dot patterns. From each dot, you can move to any other dot except those that require jumping through unvisited intermediate dots.
example_3.py — Edge Case
$
Input:
m = 3, n = 3
›
Output:
320
💡 Note:
Only counts patterns of exactly length 3. Many more possibilities exist since by length 3, intermediate dots for jumps are more likely to have been visited.
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
Visualization
Tap to expand
Understanding the Visualization
1
Setup Grid
Create 3×3 grid with numbered dots 1-9, establishing jump rules for invalid moves
2
Identify Symmetries
Recognize that corners, edges, and center have equivalent movement patterns due to rotational symmetry
3
Compute Representatives
Calculate all valid patterns starting from one corner (1), one edge (2), and center (5)
4
Apply Multipliers
Multiply by symmetry factors: corner patterns×4 + edge patterns×4 + center patterns×1
Key Takeaway
🎯 Key Insight: Exploit rotational symmetry of the 3×3 grid to reduce computation from 9 to 3 representative DFS calculations, achieving a 4x performance improvement while maintaining perfect accuracy.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code