935. Knight Dialer
Description
The chess knight has a unique movement ,it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L ). The possible movements of chess knight are shown in this diagram:
A chess knight can move as indicated in the chess diagram below:
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
Given an integer n
, return how many distinct phone numbers of length n
we can dial.
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1
jumps to dial a number of length n
. All jumps should be valid knight jumps.
As the answer may be very large, return the answer modulo 10^9 + 7
.
Example 1:
1 | Input: n = 1 |
Example 2:
1 | Input: n = 2 |
Example 3:
1 | Input: n = 3131 |
Constraints:
1 <= n <= 5000
Hints/Notes
- 2025/04/18 Q2
- matrix quick power, use dp next time
- 0x3Fâs solution
Solution
Language: C++
1 | class Solution { |