Knight Dialer
Last updated
Last updated
A chess knight can move as indicated in the chess diagram below:
.
This time, we place our chess knight on any numbered key of a phone pad (indicated above), and the knight makesN-1
hops. Each hop must be from one key to another numbered key.
Each time it lands on a key (including the initial placement of the knight), it presses the number of that key, pressingN
digits total.
How many distinct numbers can you dial in this manner?
Since the answer may be large,output the answer modulo10^9 + 7
Example 1:
Example 2:
Example 3:
Note:
1 <= N <= 5000
这里给出一个DFS的解法,找到移动的映射之后就可以进行记忆化递归了,传递的参数为步数和当前的字母,枚举每个数字的对应情况,维度就是剩余步数和数字。
Base Case是N=0是结果为1