Problem:
On a given standard dial pad, what is the # of unique numbers that can be generated from jumping N times with the constraint that when you jump, you must move like the knight chess piece. You also cannot land on any invalid values like the X's but you may pass through them.
Dialer:
1 2 3
4 5 6
7 8 9
X 0 X
Very similar to this
Generate 10-digit number using a phone keypad
What I have so far but is crazy slow (Python 2.7)
jumpMap = {
1: [6,8],
2: [7,9],
3: [4,8],
4: [0, 3, 9],
5: [],
6: [0, 1, 7],
7: [2, 6],
8: [1, 3],
9: [2, 4],
0: [4, 6]
}
def findUnique(start, jumps):
if jumps == 1:
# Base case 1 jump
return len(jumpMap[start])
if start == 5:
return 0
sum = 0
for value in (jumpMap[start]):
sum = sum + findUnique(value, jumps-1)
return sum
I'm guessing the easiest way to optimize would to have some kind of memoization but I can't figure out how to use one given the problem constraints.